In a previous message, i was trying to figure out how to write my own custom xml log format, with header and footer. i was ultimately able to do that, without too much problem, some time and patience is all it took.
I'm posting the code here, in hope it will serve another n00b like me someday.
I was not however able to figure the proper way to use those custom headers and footers. There's probably not much to it either but i ended not using my own technique after finding out that the FileTarget implements the NLog.TargetWithLayoutHeaderAndFooter interface and allowed me to put my own text for header and footer.
****
Imports NLog
Imports NLog.Layouts
Namespace AWInterface.ErrorHandling
<LayoutRenderer("appliweb_error")> _
Public NotInheritable Class AWErrorRenderer
Inherits LayoutRenderer
Private _showSession As Boolean = False
Public Property ShowSession() As Boolean
Get
Return _showSession
End Get
Set(ByVal Value As Boolean)
_showSession = Value
End Set
End Property
Protected Overrides Sub Append(ByVal builder As System.Text.StringBuilder, ByVal logEvent As NLog.LogEventInfo)
With builder
.Append(ApplyPadding(vbTab & "<error date=""" & logEvent.TimeStamp & """>" & vbCrLf))
If Not logEvent.Message Is Nothing And ShowSession Then
.Append(ApplyPadding(vbTab & vbTab & vbTab & "<session>" & logEvent.Message & "</session>" & vbCrLf))
End If
.Append(ApplyPadding(vbTab & vbTab & "<exception>" & vbCrLf))
.Append(ApplyPadding(vbTab & vbTab & vbTab & "<message>" & logEvent.Exception.Message & "</message>" & vbCrLf))
.Append(ApplyPadding(vbTab & vbTab & vbTab & "<source>" & logEvent.Exception.Source & "</source>" & vbCrLf))
.Append(ApplyPadding(vbTab & vbTab & vbTab & "<stackTrace>" & logEvent.Exception.StackTrace & "</stackTrace>" & vbCrLf))
.Append(ApplyPadding(vbTab & vbTab & vbTab & "<innerException>"))
If Not logEvent.Exception.InnerException Is Nothing Then
.Append(ApplyPadding(logEvent.Exception.InnerException.ToString))
End If
.Append(ApplyPadding("</innerException>" & vbCrLf))
.Append(ApplyPadding(vbTab & vbTab & "</exception>" & vbCrLf))
.Append(ApplyPadding(vbTab & "</error>"))
End With
End Sub
Protected Overrides Function GetEstimatedBufferSize(ByVal logEvent As NLog.LogEventInfo) As Integer
'More or less 4k characters to display in a AW session
Return 4000
End Function
End Class
Public MustInherit Class AWLayoutBase : Implements ILayout
Private myLayout As NLog.Layout
Protected ReadOnly Property BaseLayout() As NLog.Layout
Get
Return myLayout
End Get
End Property
Public Sub New()
myLayout = New NLog.Layout
End Sub
Public Sub Close() Implements NLog.ILayout.Close
myLayout.Close()
End Sub
Public MustOverride Function GetFormattedMessage(ByVal logEvent As NLog.LogEventInfo) As String Implements NLog.ILayout.GetFormattedMessage
Public Sub Initialize() Implements NLog.ILayout.Initialize
myLayout.Initialize()
End Sub
Public Function IsVolatile() As Boolean Implements NLog.ILayout.IsVolatile
Return myLayout.IsVolatile
End Function
Public Function NeedsStackTrace() As Integer Implements NLog.ILayout.NeedsStackTrace
Return myLayout.NeedsStackTrace
End Function
Public Sub PopulateLayouts(ByVal layouts As NLog.LayoutCollection) Implements NLog.ILayout.PopulateLayouts
myLayout.PopulateLayouts(layouts)
End Sub
Public Sub Precalculate(ByVal logEvent As NLog.LogEventInfo) Implements NLog.ILayout.Precalculate
myLayout.Precalculate(logEvent)
End Sub
End Class
Public Class AWHeader
Inherits AWLayoutBase
Public Overrides Function GetFormattedMessage(ByVal logEvent As NLog.LogEventInfo) As String
Return "<?xml version=""1.0"" ?><errors>" & vbNewLine
End Function
End Class
Public Class AWFooter
Inherits AWLayoutBase
Public Overrides Function GetFormattedMessage(ByVal logEvent As NLog.LogEventInfo) As String
Return vbNewLine & "</errors>"
End Function
End Class
End Namespace
Re: Custom Layout Renderer and Custom Header and Footer
Im facing a problem that while logging informations to an xml file, i need the header and footer to append automatically between my custom tags. I followed the code what u have written but still i face the same error (No header and footer tags....)Can u send me the details on how to achieve this using programatically...(not using NLog.config)