How to log Trace messages with log4net?

前端 未结 4 564
北荒
北荒 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: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.

提交回复
热议问题