Reputation: 3309
I would like to log the numeric value corresponding to the log level when using log4net. That is, right now I am logging to database with the following command text:
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
I would like to change the string value of the logging level to the numeric. Is this possible?
Upvotes: 2
Views: 897
Reputation: 17028
I did not test this but the following should work.
You can create your own converter like this:
sealed class NumericLevelPatternConverter : PatternLayoutConverter
{
override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
// maybe you need to call ToString() on the value property
writer.Write( loggingEvent.Level.Value );
}
}
and in the configuration file:
<layout type="log4net.Layout.PatternLayout">
<converter>
<name value="levelId" />
<type value="YourNamespace.NumericLevelPatternConverter" />
</converter>
<conversionPattern value="%levelId" />
</layout>
Upvotes: 5