Writing to two locations not working - log4net

I feel this should be possible but I can only get log4net to go to one appender at a time.
<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="false">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\webclient\Admin.Web\logs\admin.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!--Database appender-->
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="10" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<connectionString value="Data Source=(localhost); Database=Error_Logs; Persist Security Info=True;User ID=;Password=" />
<commandText value="INSERT INTO [GeneralServiceLog] ([dtLog],[vchLevel],[vchLogger],[vchMessage],[vchException])
VALUES( #log_date, #log_level, #logger, #message, #ex)" />
<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="#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>
<parameter>
<parameterName value="#ex" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="AdoNetAppender" />
</root>
<Logger>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</Logger>
</log4net>
I want the ERROR level messages to go to the database and DEBUG level messages to go to the text file but all the messaging are just going to file.
What am I missing?
Thanks!

You need to configure the root logger as follows:
<root>
<level value="DEBUG" />
<appender-ref ref="AdoNetAppender" />
<appender-ref ref="RollingLogFileAppender" />
</root>
then you add a filter to the database appender:
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
Update:
The problem with your configuration is that you assign the db appender to an unnamed logger and thus your appender in all likelihood never gets called. In log4net you can assign appenders to loggers, but not to levels.

Related

Asking about log4net just write to appender when i stopped site

Currently, I already config my project, but I got the issue
I can't query any message when the site is running.
When I stop the site, I can query it on database again and I see them.
Why do I just see message when I stop the site?
This is my code.
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="asyncForwarder" />
</root>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="4096" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=***;initial catalog=***;integrated security=false;persist security info=True;User ID=***;Password=***" />
<commandText value="INSERT INTO LogAPI ([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>
<appender name="asyncForwarder" type="Log4Net.Async.ParallelForwardingAppender,Log4Net.Async">
<appender-ref ref="AdoNetAppender" />
<bufferSize value="4096" />
</appender>
</log4net>
I dont know what is correct concept on the log4net.

log4net write file is ok but cant insert to database

This is my configuration file contains log4net attributes, if I use "RollingLogFileAppender" appender it writes file correctly but I am using
"AdoNetAppender" and expect to see same string in message column of inserted row. But somehow this not work..
(table defination and query configurations are from apache documentation so it cant be wrong I though connection string is my problem, But application already have connection with the same db I also shared the connection string is being used by adonet)
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\AkisLog\\Logs.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>
<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=******\SQLTEST;initial catalog=LISANSSIZ_SANTRAL_PROJE_ONAY;persist security info=True;user id=user;password=pwd" />
<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="ALL" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
...
<connectionStrings>
<add name="TedasDbContext" connectionString="metadata=res://*/Project.csdl|res://*/Project.ssdl|res://*/Project.msl;provider=System.Data.SqlClient;provider connection string="data source=****\SQLTEST;initial catalog=LISANSSIZ_SANTRAL_PROJE_ONAY;persist security info=True;user id=user;password=pwd;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
And .cs file:
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
if (log.IsErrorEnabled)
{
log.Error("Error ocurred here");
}
if (log.IsDebugEnabled)
{
log.Debug("Debug message");
}
Your buffersize is 100, which means that messages will be written after 100 messages. Change it to 1, it will flush the messages directly to the sql database:
<bufferSize value="1" />
If that does not work, enable log4net interal debugging to see if you have connection problems. Also check if you have the latest version of log4net.

filter to not log logging entries of particular class

I have a adonet appender and rolling file appender configure for my application. What i want is a filter on my adonetappender via which i can omit logging on particular class. That class has multiple level of devugging which I needed for my file appender. so i can't change the level and have to omit via filter.
My logging config is given below
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender" >
<file value="..\\Logs\\Logfile.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>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<immediateFlush value="true" />
<bufferSize value="0" />
<connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" />
<connectionString value="data source=localhost;initial catalog=logging_db;User ID=root;Password=" />
<commandText value="INSERT INTO system_log(appname, action, level)
VALUES(#appname, #action,#level);" />
<parameter>
<parameterName value="appname" />
<dbType value="String" />
<size value="32" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="My Web Service" />
</layout>
</parameter>
<parameter>
<parameterName value="action" />
<dbType value="String" />
<size value="150" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%c{1}" />
</layout>
</parameter>
<parameter>
<parameterName value="level" />
<dbType value="String" />
<size value="10" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p" />
</layout>
</parameter>
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
My class name is MyNameSpace.Business.ConditionCompare
And in my class log4net object is declared as
private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
You don't show your file appender in the config, but what you need to do is define a logger which overrides the appenders defined in root by specifying additivity="false": this allows the logger to choose which appenders to use. As you say you want to stop logging to the database altogether, you can tell the logger just to use the file appender:
Edit: add this just under the root element:
<logger name="MyNameSpace.Business.ConditionCompare" additivity="false">
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</logger>

new rollingFileAppender created the file but never writes to it

I am trying to add a new rolling file appender to an existing log4net configuration. I have been looking at questions on S.O. and looking at the documentation however I can't seem to see where my issue is.
The problem is that it creates the file but never writes to it. All I can come up with is that somehow I have misunderstood how the hierarchies work? If I attached a debugger I can see the line get written to the console but even when I did a test deploy the log file document never has anything written to it. There are other questions about this however they all have issue with levelMin or something like that. I have tried setting my config to ALL, didn't help either.
Here is complete config so you can see what I am talking about.
<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="true">
<root>
<level value="ALL"/>
<appender-ref ref="CompassServiceTraceAppender" />
<appender-ref ref="EventLogAppender" />
<appender-ref ref="DbAppender" />
</root>
<logger name="Compass.Service.Implementation.EdiService">
<level value="ALL"/>
<appender-ref ref="EventProcessingFailureLogFileAppender" />
</logger>
<logger name="Compass.Service.Implementation.PhoneService">
<level value="ALL"/>
<appender-ref ref="LivevoxResultsReprocessorAppender"/>
</logger>
<logger name="DocumentImportLogger">
<level value="ALL"/>
<appender-ref ref="DocumentLogFileAppender" />
</logger>
<logger name="NHibernate">
<level value="ERROR"/>
<appender-ref ref="CompassServiceTraceAppender" />
<appender-ref ref="EventLogAppender" />
<appender-ref ref="DbAppender" />
</logger>
<appender name="DbAppender" type="log4net.Appender.AdoNetAppender">
<threshold value="WARN" />
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=.;initial catalog=logDb;integrated security=True;persist security info=True;" />
<commandText value="INSERT INTO ErrorLog ([Date],[Thread],[Level],[Logger],[Message],[Exception],[Environment],[Application],[User],[ObjectName],[ObjectId],[SupportId]) VALUES (#log_date, #thread, #log_level, #logger, #message, #exception, 'localhost','Compass BB', #user, #objectName, #objectId, #supportId)" />
<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>
<parameter>
<parameterName value="#exception" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout" value="%exception" />
</parameter>
<parameter>
<parameterName value="#user" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%property{CurrentUser}" />
</parameter>
<parameter>
<parameterName value="#objectName" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%property{ObjectName}" />
</parameter>
<parameter>
<parameterName value="#objectId" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%property{ObjectId}" />
</parameter>
<parameter>
<parameterName value="#supportId" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%property{SupportId}" />
</parameter>
</appender>
<appender name="CompassServiceTraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<threshold value="WARN" />
<param name="ApplicationName" value="Compass Best Benefits Service Host" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger %newline User: %property{CurrentUser} Object: %property{ObjectName} Id: %property{ObjectId} %newline %message %newline %exception"/>
</layout>
</appender>
<appender name="EventProcessingFailureLogFileAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="ALL" />
<param name="File" value="c:\logs\EventProcessingFailure_log.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<param name="maxSizeRollBackups" value="10" />
<param name="maximumFileSize" value="1000KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message %newline %exception" />
</layout>
</appender>
<appender name="LivevoxResultsReprocessorAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="ALL" />
<param name="File" value="c:\logs\Livevoxreprocessor.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<param name="maxSizeRollBackups" value="10" />
<param name="maximumFileSize" value="1000KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message %newline" />
</layout>
</appender>
<!--DocumentLogFileAppender-->
<appender name="DocumentLogFileAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="ALL" />
<param name="File" value="c:\logs\DocumentLog.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<param name="maxSizeRollBackups" value="10" />
<param name="maximumFileSize" value="1000KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message %newline" />
</layout>
</appender>
</log4net>
Here is the code example.
private static readonly HALogger Logger = new HALogger("DocumentImportLogger");
//which calls
public HALogger(string loggerName)
{
_logger = LogManager.GetLogger(loggerName);
}
//Then to log.
Logger.Info(string.Format("Checking for new documents - LastDocumentId: {0}", LastDocInfo.LastDocumentId));
Your config file looks fine to me.
The problem is that log4net silently ignores configuration errors by default and just silently fails - which is a sensible design choice for a logging framework but can be frustrating at times.
You can try enabling internal debugging and programmatically checking for configuration errors.
See the log4net faq => the relevant sections are:
"How do I enable log4net internal debugging?"
"How can I evaluate configuration errors at runtime?"
Also, please add a code example of how you obtain a logger and try writing to it.

Log4Net Appender Not Loading

Log4Net isn't able to load the appender for the DB2 connection and I cannot see why. It writes the logs to the file system fine, however, the DB2 logging isn't occurring. The version of Log4Net is v1.2.10.0.
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="IBM.Data.DB2.DB2Connection,IBM.Data.DB2, Version=8.1.2.1" />
<connectionString value="server=db2Server;database=dbName;user Id=userId1;password=password;persist security info=true" />
<commandText value="INSERT INTO GOSDDL.EXCEPTION_LOG (APP_LOG_TS,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">
<conversionPattern value="%utcdate" />
</layout>
</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>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="../logs" />
<appendToFile value="true"/>
<maximumFileSize value="512KB"/>
<maxSizeRollBackups value="2"/>
<rollingStyle value="Date"/>
<datePattern value=".yyyy-MM-dd.lo" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %l %-2p - %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="RollingFile"/>
</root>
</log4net>
At the risk of pointing out the obvious, the config file you posted does not actually use AdoNetAppender. Would this be a matter of replacing RollingFile with AdoNetAppender in the second last line?
If not, can you post the rest of the config file.

Resources