views:

116

answers:

3

What is the best way to log to a file using log4net that has the correct format (correct XML, correct timestamp format, custom data in correct format, correct attributes, basically the exact same way as XmlWriterTraceListener does it) so it can be viewed in the Service Trace Viewer Tool (SvcTraceViewer.exe)?

+1  A: 

Not an answer, but I asked a question earlier today about logging and WCF and one of the things I wanted to know was about Service Trace Viewer. All of the examples that I have seen describe the XML files consumed by Service Trace Viewer being generated via System.Diagnostics TraceSources and the System.Diagnostics XmlFileListener. Anyway, if I get any answers in my post you might find them useful.

wageoghe
I think you mean System.Diagnostics.XmlWriterTraceListener.
bitbonk
You are correct. I typed that in without verifying the exact listener name. Anyway, I am still interested in how to get output (if it is possible) from log4net, NLog, etc that is compatible with SvcTraceViewer.exe.
wageoghe
+2  A: 

If I wanted to this then I would write my custom layout. I did not (yet) look at the details but I would write a class that derives from XmlLayoutBase. I need some more time to look at the details...

You could also write your own appender but I think in this case it makes more sense to write a layout class.

Edit: Maybe writing your own appender is a good idea. In that case you could use the System.ServiceModel.Diagnostics.DiagnosticTrace class. Not sure yet though if that is the way to go. I do not have much time right now, but I will look into this.

Stefan Egli
I think it will take a considerable amount of time to completely mimic the format with all options of the XmlWriterTraceListener.
bitbonk
That is what I fear as well hence my new proposal.
Stefan Egli
What System.ServiceModel.Diagnostics.DiagnosticTrace class do you mean. In the GAC I only find one wich is assembly internal and can not be used.
bitbonk
A: 

Here is an idea:

You could write a custom log4net Appender and have it write messages (indirectly) to the XmlWriterTraceListener. Inside the Append method you simply send the message to System.Diagnostics.

Here is one example of a custom Appender.

In the example, Append is overridden. It is passed a LoggingEvent class/structure. For your purposes (to get the log4net output routed to an output format that can be read by SvcTraceViewer), you could write your output to System.Diagnostics (having first configured it to log to the XmlWriterTraceListener). You could either write using Trace.Write* methods, Trace.Trace* methods, or by TraceSources.

For TraceSources, you could consider the TraceSource name to be the same as the logger name (which is available in the LoggingEvent class/structure). So, you could configure a TraceSource in your app.config file for each log4net logger name that you want to go into the xml file. Your Append logic then might look something like this:

protected override void Append(LoggingEvent le)
{
  TraceSource ts = new TraceSource(le.LoggerName); // Not sure of logger name field in LoggingEvent
  ts.TraceEvent(LogLevelToTraceEventType(le.Level), 0, le.FormattedMessage);
}

This might give you what you want. Note that I have not actually done this, so I cannot say if it is a good idea or not, but it certainly seems like it would work.

Sorry for being brief, but am trying to finish this before have to leave.

wageoghe
Writing a custom appender (for the sole purpose of formatting) has one downside: I can not use all the other Appenders (RollingFileAppender, FileAppender, ...) or I would have to subclass all of them.
bitbonk
FWIW, you can find the code for the XmlWriterTraceListener here: http://www.koders.com/csharp/fid7ECC273D0A665AFFDB9276AC64289085CD1E2A8E.aspx?s=DateTime I'm not sure how current it is (i.e. .NET 1.1, 2.0, 3.0, etc), but it seems to show how to lay out the xml in a format that can be consumed by SvcTraceViewer. Maybe this could be used to develop an XmlLayout. I am not really very familiar with writing log4net layouts, so I can't say if this is a good idea or not.
wageoghe
A custom appender is IMHO not possible because I can not write to other appenders using that very same format. I would like to write to the RollingFileAppender using the format.
bitbonk