Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert tabs in the log4net layout pattern?

Tags:

log4net

My need is simple - I want to be able to open my text log file in excel, so that it automatically breaks it in columns matching the log fields.

For that, I need the log fields separated with a tab.

My pattern is: %utcdate [%thread] %-5level %logger - %message%newline

I need something like: %utcdate%tab[%thread]%tab%-5level%tab%logger%tab%message%newline

Thanks.

like image 891
mark Avatar asked Oct 14 '10 13:10

mark


3 Answers

Caveat: I haven't actually used log4net. But if I understand correctly, the configuration is an XML file, isn't it? And the pattern is just text with some special tokens. So have you tried embedding actual tab characters in your pattern? The XML sequence for a tab is 	, e.g.:

<conversionPattern value="%utcdate&#9;[%thread]&#9;%-5level&#9;%logger&#9;%message%newline" />

Or if you're supplying the pattern some other way (perhaps via the PatternString constructor or whatever), just include tab characters in the string you're passing in. The docs for that constructor defer to the PatternLayout docs to talk about the string itself, and there they say:

You are free to insert any literal text within the conversion pattern.

(Their emphasis.) Worth a try, anyway...

like image 72
T.J. Crowder Avatar answered Nov 20 '22 17:11

T.J. Crowder


This worked for me:

1) When your logging class is instantiated, add this line:

log4net.GlobalContext.Properties["tab"] = "\t";

2) Then in the log4net XML, make a reference to your newly-created log4net property. For example:

<conversionPattern value="%property{tab}%message%newline" />
like image 9
Gordon Slysz Avatar answered Nov 20 '22 17:11

Gordon Slysz


You can type tab into pattern without escaping (see space between level and date):

<conversionPattern value="%level    %date{HH:mm:ss,fff} ..." />

I cant write tab in visual studio, because it writes spaces, but I typed tab into notepad++ copy it (ctrl+c ctrl+v) and it is working.

EDIT: stack overflow replaced my tab with spaces. So you need to type your own tab

like image 2
Peter Dub Avatar answered Nov 20 '22 19:11

Peter Dub