Michael Dausmann
Michael Dausmann

Reputation: 4540

How do I Log the Class file without the path in log4net

I want to be able to log the class file and line number in my log file so I am using the following config...

<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 is making my log file entries waaaay 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 just the class file ('Controller.cs') instead of the full path to the file also???

Michael

Upvotes: 3

Views: 2835

Answers (2)

bryanbcook
bryanbcook

Reputation: 18023

Although you're asking about the file name, I believe you can use %type to get the fully qualified type name (a.b.className). If you just want the class name, use %type{1}

Note that any method that generates caller information (%file and %type) have a performance cost associated with them.

As an aside, you can bypass the performance hit by naming the Logger using the Type name.

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

Your conversion pattern would look like:

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

Where your logger would be "MyNameSpace.Foo". Likewise, if you only want the class name, use "%logger{1}, which will resolve as "Foo".

One of the best advantages to this approach is that you can leverage log4net's hierarchical repository system to adjust logging levels per type:

<!-- 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>

Upvotes: 6

Peter Lillevold
Peter Lillevold

Reputation: 33910

Out of the box, PatternLayout supports only the %file token. What you can do is subclass PatternLayout and add your own pattern, say %filename, and for this token only output the name of the file.

Upvotes: 2

Related Questions