开发者

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


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>


Don't forget these using statements:

using log4net.Layout.Pattern;
using log4net.Core;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜