How to log Trace messages with log4net?

前端 未结 4 432
北荒
北荒 2020-11-30 21:12

I\'m using log4net to log write log message to a rolling log file.

Now I would also redirect all trace messages from System.Diagnostics.Trace to that l

相关标签:
4条回答
  • 2020-11-30 21:35

    Thanks,

    I went with Dirk's answer slimmed down a bit.

    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 讨论(0)
  • 2020-11-30 21:42

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

    The TraceListener doesn't have too many method that needs to be implemented and all you would do is to forward the values to log4net so this should be easy to do.

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

    0 讨论(0)
  • 2020-11-30 21:49

    As per the answers above, there's an implementation here (this link is flaky, but I did find the source code):

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

    To crudely deal with the issue (described in the comments to a previous answer) of internal log4net trace issuing from the LogLog class, I checked for this class being the source of the trace by inspecting the stack frame (which this implementation did already) and ignoring those 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 a TraceAppender will still create the problems described in the comments above.

    0 讨论(0)
  • 2020-11-30 21:55

    According to Rune's suggestion I implemented a basic TraceListener which output 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);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题