Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net: how to customize data to be stored

I have few different web services exposed by my web site. Here is a goals I want to reach:

  1. Store all data in DB in one table;
  2. Each record should have text field with 'service/component' name;
  3. It is necessary to store all income parameters when any method is called;
  4. It is necessary to store all outcome parameters AND error messages if any exception is occurred.

My original intention was to use log4net. But from these walk-through (http://sadi02.wordpress.com/2008/09/15/how-to-store-log-in-database-using-log4net/, http://logging.apache.org/log4net/release/config-examples.html) I don't see an easy way to add custom data into log table.

Is it feasible to use log4net for such purposes? How can I change logging table schema to store custom data? Should I modify source code of log4net library? Probably would be better to write a custom functionality to store such kind of data?

Thanks.

like image 271
Budda Avatar asked Nov 29 '10 21:11

Budda


People also ask

What is log4net config?

The log4net configuration can be configured using assembly-level attributes rather than specified programmatically. If specified, this is the filename of the configuration file to use with the XmlConfigurator. This file path is relative to the application base directory (AppDomain. CurrentDomain.

Where is log4net config?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.

Is log4net dependent on log4j?

Log4net is a logging utility for . NET applications. It's based on log4j, which is for Java applications. Log4net is highly configurable, so you can use it in many scenarios.

Is log4net thread safe?

Is log4net thread-safe? Yes, log4net is thread-safe.


2 Answers

According to this discussion, the process is pretty straightforward.

The first step is to update the command text in the appender declaration into your config file:

<commandText value="INSERT INTO Log4Net ([Date],[Thread],[Level],[Logger],[Message],[Exception],[MyColumn]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception,@MyColumn)"/>        

The next step is to and add a new parameter definition for the custom column:

<parameter>
   <parameterName value="@MyColumn "/>
   <dbType value="String" />
   <size value="255" />
   <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{MyColumn}" />
   </layout>
</parameter>

Finally, you can access the value through the GlobalContext properties of log4net:

log4net.GlobalContext.Properties["MyColumn"] = "MyValue";
log.Debug("My message");

You can do this for as many fields as you need.

like image 69
Dillie-O Avatar answered Sep 28 '22 02:09

Dillie-O


Neither of suggested answers works for me. The problem is that I need to have several custom fields. For me it seems too complicated to create different instances of logger or to setup column values each time through the 'properties'.

I will go (actually, already implemented) custom logger that put data in DB.

like image 34
Budda Avatar answered Sep 28 '22 02:09

Budda