5

My log4net conversion pattern looks like this

<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />

The %file spits out the full path covering almost one full line in my console window.

How can I get just the file name (minus path).

Right now it looks like this

INFO [10] <c:\My Root Dir\Subdir\...........................\filename.cs> - My message

I want it to look like

INFO [10] <filename.cs> - My message

thank you

Gullu
  • 3,477
  • 7
  • 43
  • 70

2 Answers2

4

You can write your own pattern layout converter, maybe like this:

public class FileNamePatternConverter : PatternLayoutConverter
{       
    override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        writer.Write(Path.GetFileName(loggingEvent.LocationInformation.FileName));
    }
}

Then you configure it as follows:

<conversionPattern value="%5level [%thread] (%filename:%line) - %message%newline"" />
   <converter>
   <name value="filename" />
   <type value="YourNamespace.FileNamePatternConverter" />
</converter>
Stefan Egli
  • 17,398
  • 3
  • 54
  • 75
1

Don't forget these using statements:

using log4net.Layout.Pattern;
using log4net.Core;
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Alexander
  • 342
  • 4
  • 14