How to log Trace messages with log4net?

I am using log4net to register a log entry in a moving log file.

Now I would also redirect all trace messages from System.Diagnostics.Trace to this log file. How can I customize this? I tried to find something about this in the log4net documentation, but to no avail. Is it possible at all?

I want to do this because I'm interested in third-party Trace messages.

 <log4net> <appender name="R1" type="log4net.Appender.RollingFileAppender"> <file value="C:\Logs\MyService.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <maxSizeRollBackups value="10" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> </log4net> 
+67
c # log4net
Feb 05 '09 at 10:44
source share
4 answers

As per Rune's suggestion, I implemented a basic TraceListener that outputs to log4net:

 public class Log4netTraceListener : System.Diagnostics.TraceListener { private readonly log4net.ILog _log; public Log4netTraceListener() { _log = log4net.LogManager.GetLogger("System.Diagnostics.Redirection"); } public Log4netTraceListener(log4net.ILog log) { _log = log; } public override void Write(string message) { if (_log != null) { _log.Debug(message); } } public override void WriteLine(string message) { if (_log != null) { _log.Debug(message); } } } 
+66
Feb 05 '09 at 11:39
source share

I don't know if log4net supports this, but you can implement your own trace listener that did this.

There are not too many methods in TraceListener that need to be implemented, and all you have to do is forward the values ​​to log4net so that it is easy to do.

To add a custom trace listener, you either modify your app.config / web.config file or add it to your code using Trace.Listeners.Add(new Log4NetTraceListener());

+25
Feb 05 '09 at 10:49
source share

In accordance with the above answers, the implementation is implemented here (this link is fuzzy, but I found the source code):

https://code.google.com/archive/p/cavity/

To roughly solve the problem (described in the comments to the previous answer) of the internal log4net trace coming from the LogLog class, I checked that this class is the source of the trace by checking the stack frame (which this implementation has already done) and ignoring these trace messages:

  public override void WriteLine(object o, string category) { // hack to prevent log4nets own diagnostic trace getting fed back var method = GetTracingStackFrame(new StackTrace()).GetMethod(); var declaringType = method.DeclaringType; if (declaringType == typeof(LogLog)) { return; } /* rest of method writes to log4net */ } 

Using TraceAppender will still create the problems described in the comments above.

+1
Jun 08 '16 at 10:07 on
source share

Thank,

I went with Dirk, the answer was a little thinner.

 public class Log4netTraceListener : System.Diagnostics.TraceListener { private readonly log4net.ILog _log; public Log4netTraceListener() : this(log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)) { } public Log4netTraceListener(log4net.ILog log) { _log = log; } public override void Write(string message) => _log?.Debug(message); public override void WriteLine(string message) => _log?.Debug(message); } 
0
Feb 03 '19 at 10:40
source share



All Articles