Which log4net template provides a file name without a full path

My log4net conversion template is as follows

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

The% file splashes out the full path, covering almost one full line in the console window.

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

Now it looks like

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

+5
source share
2 answers

You can write your own template layout converter, perhaps 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 like this:

<conversionPattern value="%5level [%thread] (%filename:%line) - %message%newline"" />
   <converter>
   <name value="filename" />
   <type value="YourNamespace.FileNamePatternConverter" />
</converter>
+2
source

Do not forget about it with the help of operators:

using log4net.Layout.Pattern;
using log4net.Core;
+1
source

All Articles