I have few different web services exposed by my web site. Here is a goals I want to reach:
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.
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.
You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.
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? Yes, log4net is thread-safe.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With