Code Coverage by Clover.NET

Custom XML log file

View: New views
4 Messages — Rating Filter:   Alert me  

Custom XML log file

by Eniac :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I've downloaded and installed NLog.

Now im playing around with it, trying to get a customized format for my XML log and i find myself unable to do anything, anything useful for my application anyway.

I've tried the log4jxmlevent renderer and it works fine but not completely. it doesn't render proper xml file since its missing header and footer.

I've tried to make some combination with LayoutWithHeaderAndFooter but i cannot make any kind of sense on how to use it.

I've thought of creating my own renderer, which would work out fine i think, but i woud still need to make it work with LayoutWithHeaderAndFooter.

Now, im trying to instantiate the Header and footer thing and do it programatically but apparently i need classes that implements ilayout. when i try to create my own, i have no idea what kind of work the methods are supposed to accomplish.

all i really wanna do is something like this :

Dim myLayout as New myCustomRenderer()

dim myHeaderAndFooter as new NLog.LayoutWithHeaderAndFooter
myHeaderAndFooter.Header = "<?xml version="""1.0""" ?>" & vbcrlf & vbtab & "<errors>"
myHeaderAndFooter.Footer = vbtab & "</errors>"
myHeaderAndFooter.Layout = myLayout


**
but i know its not possible, can please someone point me in the right direction ? im really having problem finding documention on the matter.

Re: Custom XML log file

by Jaroslaw_Kowalski :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Try this. Note that you have to quote opening and closing tag's triangle brackets using < and > but other than that it should should work in a natural way.

<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target xsi:type="ColoredConsole" name="console">
      <layout xsi:type="LayoutWithHeaderAndFooter">
        <header xsi:type="SimpleLayout" text="<root>"/>
        <layout xsi:type="Log4JXmlEventLayout" />
        <footer xsi:type="SimpleLayout" text="</root>"/>
      </layout>
    </target>
  </targets>

  <rules>
    <logger name="*" writeTo="console" />
  </rules>
</nlog>

Re: Custom XML log file

by Eniac :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.



Re: Custom XML log file

by Jaroslaw_Kowalski :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Or you can do what I did - escape < and > characters with &lt; and &gt; respectively.