Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net not logging ThreadContext

I have log4net running on my AsP.NET site. I'm able to log messages to my DB Table, but it isn't logging the ThreadContext properties. For example:

ThreadContext.Properties["Url"] = HttpContext.Current.Request.Url.ToString();
ThreadContext.Properties["HttpReferer"] = HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];

My log4net.config adds those values as parameters into my SQL DB table:

<parameter>
    <parameterName value="@URL"/>
    <dbType value="String"/>
    <size value="512"/>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{log4net:Url}"/>
    </layout>
</parameter>
<parameter>
    <parameterName value="@HttpReferer"/>
    <dbType value="String"/>
    <size value="512"/>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{log4net:HttpReferer}"/>
    </layout>
</parameter>

As I debug, I see that those ThreadContext properties are being set, but they aren't getting into the DB.

How can I get that to work?

like image 687
sgwill Avatar asked Oct 09 '08 13:10

sgwill


People also ask

What is the use of context properties in log4net?

Context Properties The log4net contexts store properties, i.e. name value pairs. The name is a string the value is any object. A property can be set as follows: log4net.


1 Answers

So, it turns out the config was to blame. It was slightly wrong:

Original:

<conversionPattern value="%property{log4net:HttpReferer}"/>

Changed:

<conversionPattern value="%property{HttpReferer}"/>

I had to take out the "log4net:" inside of property.

What's odd is that one property still required log4net:propertyName. I have absolutely no idea why it works this way, but that's the fix that worked!

like image 170
sgwill Avatar answered Sep 19 '22 00:09

sgwill