Re: Custom XML log file
Ah, well, that certainly put me on the right track.
It turns out however that i cannot put the xml in the header and footer elements, i get complaints from .net about invalid characters.
so.. what i did, i partially put it in the web.config and partially programmatically, like so:
<nlog>
<target xsi:type="File"
name="file"
fileName="${logDirectory}/AW_Errors_${shortdate}.xml"
encoding="utf-8" />
</targets>
</nlog>
then in the code
'Step 1. Create configuration object
Dim config As LoggingConfiguration = LogManager.Configuration()
' Step 2. Create targets and add them to the configuration
Dim fileTarget As fileTarget = config.FindTargetByName("file")
'config.AddTarget("file", fileTarget)
' Step 3. Set target properties
fileTarget.Header = "<?xml version=""1.0""?>" & vbNewLine & "<errors>"
FileTarget.Layout = "${appliweb_error}"
FileTarget.Footer = "</errors>"
And that works.... almost! im missing the footer.
from the log file :
<?xml version="1.0"?>
<errors>
<error date="2008-02-01 2:20:48 PM">...blah...</error>
<error date="2008-02-01 2:20:48 PM">... blah ...</error>
as you can see, its missing the footer. and i understand that its related to the fact that appending an error deletes the first footer or something like that, but i cant figure out how to make it work. im sure there's a way otherwise the property wouldn't be there. but every combinations ive tried doesn't work.
the only one that did work is when setting the property ReplaceFileContentOnEachWrite - which is obviously not good because i end up with only one error in my log file.
Can you explain me how to get around that ?
<edit>
I found out that after some time, NLog writes the footer to the file, maybe when the thread becomes inactive, im not too sure, but i reopened the log file and the footer was there.
</edit>
****
for the record, i was able to create my customized ILayout header and footer, i just ended up not using it since FileTarget supports what i need. ill post it in a new thread, hopefully it will give a work base if someone tries to find out how to achieve it.
|