You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by "Smith, Johnathon (KEYPEOPLE RESOURCES INC)" <jo...@eds.com> on 2005/08/16 16:41:33 UTC

Custom parameters

I am new to Log4net, so forgive me if this has been answered, but I
could not find an answer to my question in the documentation or mailing
lists.

I saw one post in the mailing list that had custom parameters for the
AdoNetLogger and I want to do the same thing.  For example, every sample
I see looks something like the sample I copied below.  But, if I wanted
my own custom parameter such as CustomerID, I would create a parameter
like so:
	<parameter>
		<parameterName value="@CustID" />
		<dbType value="String" />
		<size value="255" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern
value="%I_DONT_KNOW_WHAT_GOES_HERE" />
		</layout>
	</parameter>
... and I would change the database to add the column and change the
insert statement accordingly.  All that is fine.

So, finally the question, how do get the appender to know what the
CustomerID is?  How do I get it to pass the customer id to the appender?
Could someone give me a sample of how to get something other than the
basic properties such as thread, log level, or message?  Or better yet,
if this is documented somewhere, could you point me to it?

Thanks for any help,





Sample AdoNetAppender 

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
	<bufferSize value="100" />
	<connectionType value="System.Data.SqlClient.SqlConnection,
System.Data, Version=1.0.3300.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
	<connectionString value="data source=[database server];initial
catalog=[database name];integrated security=false;persist security
info=True;User ID=[user];Password=[password]" />
	<commandText value="INSERT INTO Log
([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES
(@log_date, @thread, @log_level, @logger, @message, @exception)" />
	<parameter>
		<parameterName value="@log_date" />
		<dbType value="DateTime" />
		<layout type="log4net.Layout.RawTimeStampLayout" />
	</parameter>
	<parameter>
		<parameterName value="@thread" />
		<dbType value="String" />
		<size value="255" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%thread" />
		</layout>
	</parameter>
	<parameter>
		<parameterName value="@log_level" />
		<dbType value="String" />
		<size value="50" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%level" />
		</layout>
	</parameter>
	<parameter>
		<parameterName value="@logger" />
		<dbType value="String" />
		<size value="255" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%logger" />
		</layout>
	</parameter>
	<parameter>
		<parameterName value="@message" />
		<dbType value="String" />
		<size value="4000" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%message" />
		</layout>
	</parameter>
	<parameter>
		<parameterName value="@exception" />
		<dbType value="String" />
		<size value="2000" />
		<layout type="log4net.Layout.ExceptionLayout" />
	</parameter>
</appender>

Re: Custom parameters

Posted by Ron Grabowski <ro...@yahoo.com>.
You could store the value of CustomerID inside a context such as
ThreadContext:

 log4net.ThreadContext.Properties["CustomerID"] = customerId;

Or you could write your own logger:

http://tinyurl.com/b2lxq
http://www.mail-archive.com/log4net-user%40logging.apache.org/msg01993.html

that has overloads for passing in a CustomerID:

 log.Info(customerId, "Hello World");

This is how the parameter node would retrieve the value:

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

This page shows all the available patterns:

http://tinyurl.com/e3nd3
http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html

--- "Smith, Johnathon (KEYPEOPLE RESOURCES INC)"
<jo...@eds.com> wrote:

> I am new to Log4net, so forgive me if this has been answered, but I
> could not find an answer to my question in the documentation or
> mailing
> lists.
> 
> I saw one post in the mailing list that had custom parameters for the
> AdoNetLogger and I want to do the same thing.  For example, every
> sample
> I see looks something like the sample I copied below.  But, if I
> wanted
> my own custom parameter such as CustomerID, I would create a
> parameter
> like so:
> 	<parameter>
> 		<parameterName value="@CustID" />
> 		<dbType value="String" />
> 		<size value="255" />
> 		<layout type="log4net.Layout.PatternLayout">
> 			<conversionPattern
> value="%I_DONT_KNOW_WHAT_GOES_HERE" />
> 		</layout>
> 	</parameter>
> ... and I would change the database to add the column and change the
> insert statement accordingly.  All that is fine.
> 
> So, finally the question, how do get the appender to know what the
> CustomerID is?  How do I get it to pass the customer id to the
> appender?
> Could someone give me a sample of how to get something other than the
> basic properties such as thread, log level, or message?  Or better
> yet,
> if this is documented somewhere, could you point me to it?
> 
> Thanks for any help,