pyram
pyram

Reputation: 935

How to setup log4net so that it doesn't log some attributes?

I'm working with a solution on visual studio 2008, .net framework 3.5, c#, windows 7. I've created a log4net library that writes to a file like this:

2014-11-11 16:33:31,387 [7] DEBUG Utilities.Log.Debug():24 - Message from App.

I want to remove some attributes when it writes so that it writes like this:

2014-11-11 16:33:31,387 - Message from App.

Basically I want to remove the [7] number, the Project name and method name, and the code line where the method is located in the App.

I've searched the documentation but I can't find anything related to this problem.

What can i do?

thanks.

Upvotes: 1

Views: 110

Answers (2)

hunch_hunch
hunch_hunch

Reputation: 2331

Your log4net configuration file (e.g., Log4Net.config) should contain at least one <appender> section with <conversionPattern> nested under <layout>. For example, to log to the console:

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  <target value="Console.Out" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
</appender>

Your <conversionPattern> should simply have value="%date - %message%newline" if all you want is the date and the logged message.

See the PatternLayout Class documentation for more information.

Upvotes: 2

SmartDev
SmartDev

Reputation: 2862

Search your .config file for log4net.Appender.FileAppender. You should have something like this:

<log4net>
    <appender name="TextFileAppender" type="log4net.Appender.FileAppender">
        <file value="Logs/Logger.log" />
        <appendToFile value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date{dd.MM.yyyy HH:mm:ss} [%identity] %-5level %class.%method: %message%newline" />
        </layout>
    </appender>
</log4net>

Check the conversionPattern and change it in order to change the logged message.

Upvotes: 1

Related Questions