I am testing Log4Net and have been able to write to text files, event viewers. I was trying to test it with MS SQL 2008 Express and I cannot seem to get it to insert into the DB I have created locally.
Instance: (local)
DB Name: Log4Net
I have omitted the User Id and Password on the connection string in the config below as I am running it locally:
<log4net debug="true">
<!--
APPENDING TO A TEXT FILE
-->
<!--<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\Log4Net\\TestLog.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>-->
<!--
WRITING TO THE EVENT LOG
-->
<!--<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="Log4Net Test" />
<layout type="log4net.Layout.PatternLayout">
--><!--<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />--><!--
<conversionPattern value="%date - %message%newline" />
</layout>
</appender>-->
<!--
WRITING TO MS SQL
-->
<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=(local);initial catalog=Log4Net;integrated security=false;persist security info=True" />
<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>
<root>
<level value="DEBUG" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
Here is the test C# code:
if (log.IsDebugEnabled)
{
log.Debug("Insert some useful comment..");
}
if (log.IsErrorEnabled)
{
log.Error("Some Error blah blah.. Testing Only..");
}
P.S.
I definitely have created the 'Log' table already.
Link to config examples: http://logging.apache.org/log4net/release/config-examples.html
Thanks in advance
If you want to omit user id and password from the connection string, you need to set integrated security=true.
Related
I am trying to log events in a mySQL database using a log4net appender but I do not manage to configure log4net file correctly,
Here is the log4net.config:
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="1"/>
<lossy value="false"/>
<connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
<!--connectionString value="Server=my_server_ip_or_name;Database=my_db_name; Uid=my_user_name;Pwd=my_passwd;"/-->
<connectionString value="server=my_server_ip_or_name;database=my_db_name; user=my_user_name;password=my_passwd;port=3306"/>
<commandText value="INSERT INTO Log (dtLog,lbThread,lbLevel,lbLogger,lbMessage,lbException,lbContext) VALUES (#log_date, #thread, #log_level, #logger, #message, #exception, #context)"/>
<parameter>
<parameterName value="#log_date"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
<parameter>
<parameterName value="#thread"/>
<dbType value="String"/>
<size value="32"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%t"/>
</layout>
</parameter>
<parameter>
<parameterName value="#log_level"/>
<dbType value="String"/>
<size value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p"/>
</layout>
</parameter>
<parameter>
<parameterName value="#context"/>
<dbType value="String"/>
<size value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%x"/>
</layout>
</parameter>
<parameter>
<parameterName value="#logger"/>
<dbType value="String"/>
<size value="512"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%c"/>
</layout>
</parameter>
<parameter>
<parameterName value="#message"/>
<dbType value="String"/>
<size value="4000"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m"/>
</layout>
</parameter>
<parameter>
<parameterName value="#exception"/>
<dbType value="String"/>
<size value="4000"/>
<layout type="log4net.Layout.ExceptionLayout"/>
</parameter>
</appender>
At runtime, I get the following exception:
{"Object reference not set to an instance of an object. (C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Config\\machine.config line 295)"}
Which corresponds to the following config line:
<add name="MySqlSiteMapProvider" type="MySql.Web.SiteMap.MySqlSiteMapProvider, MySql.Web, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" />
The debug of log4net logs the following error:
iisexpress.exe Error: 0 : Authentication to host 'my_server_ip_or_name' for user 'my_user_name' using method 'mysql_native_password' failed with message: Access denied for user 'my_user_name'#'my_local_machine_name' (using password: YES)
log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Could not open database connection [server=my_server_ip_or_name;database=my_db_name; user=my_user_name;password=my_passwd;port=3306]. Connection string context [ConnectionString].
MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host 'my_server_ip_or_name' for user 'my_usr_name' using method 'mysql_native_password' failed with message: Access denied for user 'my_user_name'#'my_local_machine_name' (using password: YES) ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Access denied for user 'my_user_name'#'my_local_machine_name' (using password: YES)
I do not understand why log4net tries to connect to my local computer instead of the server I configured.
Any idea of what is wrong in my config?
As #TZHX noticed, I had to grant remote access for the mySQL user at the remote IP:
GRANT ALL ON my_db_name.* TO my_user_name#'my_remote_ip' IDENTIFIED BY 'my_passwd';
I also had to comment the MySql sitemap config in the machine.config file:
<!--add name="MySqlSiteMapProvider" type="MySql.Web.SiteMap.MySqlSiteMapProvider, MySql.Web, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" /-->
While it is not difficult to setup different log files, each with a different conversionPattern, per level, I would like to have minimal logging for all but errors, where I'd like a detailed log entry. Here's a snippet of my current configuration:
<appender name="WarningsAndBelowFileAppender" type="log4net.Appender.FileAppender">
<file value="log.txt" />
<appendToFile value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMax value="WARN" />
</filter>
</appender>
<appender name="ErrorsFileAppender" type="log4net.Appender.FileAppender">
<file value="errors.txt" />
<appendToFile value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
<threshold value="ERROR" />
</appender>
I'd love to have both of these configurations go into a single log file, just have different conversionPatterns for each. Can this be done?
you can't do it within the config file but you can make it with pattern layout converter.
See information here
This should be the converted code:
class MyMessageConverter : PatternLayoutConverter
{
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
if (loggingEvent.Level.Value <= Level.Warn.Value)
{
writer.Write(loggingEvent.RenderedMessage);
}
else
{
writer.Write("{0} [{1}] {2,5} {3} - {4}",
loggingEvent.TimeStamp,
loggingEvent.ThreadName,
loggingEvent.Level.Name,
loggingEvent.LocationInformation.ClassName,
loggingEvent.RenderedMessage);
}
}
}
And here is the config file:
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log.txt" />
<appendToFile value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%MSG%n" />
<converter>
<name value="MSG" />
<type value="yournamespace.MyMessageConverter " />
</converter>
</layout>
</appender>
I am inserting JSON format data in sql database using log4net. Everything is fine except custom properties which are not being saved.
This is my configuration:
<appender name="TGGADONetAppenderjson" type="log4net.Appender.ADONetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=LOANER-1122-HP\SQLEXPRESS;Initial Catalog=CAS-Dev;integrated security=false;persist security info=True;User Id=sa;Password=abinash12345;" />
<commandText value="INSERT INTO Log ([Message],[AppName],[TransactionId]) VALUES
(#message, #appName,#transactionId)" />
<parameter>
<parameterName value="#message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json"></layout>
</parameter>
<parameter>
<parameterName value="#appName" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.PatternLayout">
<!--<conversionPattern value="%property{Environment}" />-->
<conversionPattern value="APPNAME-LogTest" />
<!--should be a fixed value-->
</layout>
</parameter>
<parameter>
<parameterName value="#transactionId" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.PatternLayout" >
<conversionPattern value="%property{TransactionId}" />
</layout>
</parameter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="TGGADONetAppenderjson" />
</root>
When I debug, I can see the values of the custom property "TransactionId" is being put into the thread. However the insert does not work. The TransactionId is not logged in the message nor inserted into the column TransactionId.
I am using log4net json version 1.2.13.29 from NuGet
There's some more configuration to go into the SerializedLayout. You need to explicitly name the property. Try something like:
<layout type='log4net.Layout.SerializedLayout, log4net.Ext.Json'>
<decorator type='log4net.Layout.Decorators.StandardTypesDecorator, log4net.Ext.Json' />
<default /> <!-- explicit default members -->
<member value='TransactionId' /> <!-- explicit property reference -->
</layout>
Check the Members test case that might help you.
Alternatively, serialize all the properties by adding a properties member.
I have a application where simple text file logging is working fine. now i also needed to logging in mysql database.
But nothing happened after my log entries of code.
My log4net configuration is
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<immediateFlush value="true" />
<bufferSize value="1" />
<connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.4.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
<connectionString value="data source=localhost;initial catalog=mytestdb;User ID=root;Password=" />
<commandText value="INSERT INTO system_log(thread,level,appname,message,action) VALUES (?thread, ?level, ?appname, ?message, ?action)" />
<parameter>
<parameterName value="thread" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%t" />
</layout>
</parameter>
<parameter>
<parameterName value="level" />
<dbType value="String" />
<size value="10" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p" />
</layout>
</parameter>
<parameter>
<parameterName value="appname" />
<dbType value="String" />
<size value="10" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="My Web Service" />
</layout>
</parameter>
<parameter>
<parameterName value="message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
</parameter>
<parameter>
<parameterName value="action" />
<dbType value="String" />
<size value="45" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5c{1}.%M" />
</layout>
</parameter>
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
</appender>
<!--<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender" >
<file value="..\\Logs.txt" />
<appendToFile value="true" />
<datePattern value="yyyyMMdd" />
<rollingStyle value="Date" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.fff} %10p %-5property{_App} %-5property{_Node} [%2t] %5c{1}.%M - %m%n" />
</layout>
</appender>-->
<root>
<level value="ALL" />
<!--<appender-ref ref="RollingLogFileAppender" />-->
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
In current configuration buffersize was initially set to 100.. So I have to set it to 0 or 1 so that log4net immediately send message.
<bufferSize value="0" />
Second Problems was i were using commandparamter in wrong way
I have set it to
<commandText value="INSERT INTO system_log(thread_id,level,appname,message,action) VALUES (#threadParam,#levelParam,#appnameParam,#messageParam,#actionParam);" />
which works
I'd like to encrypt the connection string used by the log4net AdoNetAppender.
Can this be done without encrypting the entire appender?
Dion Olsthoorn blogged about setting the connection string in code, but I'd prefer to do it in a config file that is dedicated to logging as it will be using a different database to the rest of the application.
Below is the sample config from http://logging.apache.org/log4net/release/sdk/log4net.Appender.AdoNetAppender.html
<appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=SQLSVR;initial catalog=test_log4net;integrated security=false;persist security info=True;User ID=sa;Password=sa" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (#log_date, #thread, #log_level, #logger, #message)" />
<parameter>
<parameterName value="#log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
</parameter>
<parameter>
<parameterName value="#thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout" value="%thread" />
</parameter>
<parameter>
<parameterName value="#log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%level" />
</parameter>
<parameter>
<parameterName value="#logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout" value="%logger" />
</parameter>
<parameter>
<parameterName value="#message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout" value="%message" />
</parameter>
</appender>
The current source already have the ConnectionStringName property that does exactly what you are looking for.
The ConnectionStringName property was introduced in revision 607748. Version 1.2.10 is revision 395324 which is quite old compared to that.
If you're comfortable working with unreleased code you could get the latest source and compile it yourself. You could probably also get just the updated AdoNetAppender class though it could have dependencies on other updates to the log4net core.
I've been able to work around this by inheriting from the log4net AdoNetAppender and adding a property called ConnectionStringName.
If this is set it will read the connection string from the web.config connection strings and pass it through to the underlying AdoNetAppender.
I'd still be keen for a solution that keeps all the logging configuration in a single file and still allows the connection string to be encrypted.
public class ConfigAdoNetAppender : AdoNetAppender
{
public string ConnectionStringName
{
set
{
this.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[value].ToString();
}
}
}