Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What log4net pattern provides for filename without the full path

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

like image 818
Gullu Avatar asked Oct 25 '22 04:10

Gullu


1 Answers

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>
like image 65
Stefan Egli Avatar answered Nov 08 '22 10:11

Stefan Egli