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)?
views:
116answers:
3Not 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.
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.
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.