How to write class file without path in log4net

I want to be able to register the class file and line number in my log file, so I use the following configuration ...

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <!--etc-->
   <layout type="log4net.Layout.PatternLayout">
       <conversionPattern value="%date [%thread] %-5level (%file:%line) %logger => %message%newline" />
  </layout>
</appender>

However, the% file attribute makes my waaaay log file entries too long to read comfortably ....

2009-08-07 16:41:55,271 [7] INFO  (O:\mystream\aevpallsrv\DotNet\com.mycompany.au\myapp\Myappp\Controller.cs:75) MyApp.Controller => Controller.EnqueueWorkerThreads() - START

Is there a way to show only the class file ('Controller.cs') instead of the full path to the file?

Michael

+3
source share
2 answers

Although you are asking about the file name, I think you can use % type to get the full type name (abclassName). If you just want a class name, use % type {1}

, , (% file % type), .

, , Logger, .

namespace MyNamespace
{     
    public class Foo
    {
        private static ILog log = LogManager.GetLogger(typeof(Foo));
    }
}

:

"%date [%thread] %-5level %logger %message"

"MyNameSpace.Foo". , , "% logger {1}, " Foo ".

, log4net :

<!-- all classes in MyNamespace are warn -->
<logger name="MyNamespace">
     <level value="WARN" />
</logger>

<!-- only Foo is in debug -->
<logger name="MyNamespace.Foo">
    <level value="DEBUG" />
</logger>
+5

PatternLayout % . , PatternLayout , , %, .

+2

Source: https://habr.com/ru/post/1714587/


All Articles