I have to log thread context properties like this:
string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "log4netconfig.xml";
FileInfo finfo = new FileInfo(logFilePath);
log4net.Config.XmlConfigurator.ConfigureAndWatch(finfo);
ILog logger = LogManager.GetLogger("Exception.Logging");
log4net.ThreadContext.Properties["MESSAGE"] = exception.Message;
log4net.ThreadContext.Properties["MODULE"] = "module1";
log4net.ThreadContext.Properties["COMPONENT"] = "component1";
logger.Debug("test");
and the configuration file is:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<logger name="Exception.Logging" level="Debug">
<appender-ref ref="AdoNetAppender"/>
</logger>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<connectionString value="Data Source=xe;User ID=test;Password=test;" />
<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<bufferSize value="10000"/>
<commandText value="Log_Exception_Pkg.Insert_Log" />
<commandType value="StoredProcedure" />
<parameter>
<parameterName value="#p_Error_Message" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{MESSAGE}"/>
</layout>
</parameter>
<parameter>
<parameterName value="#p_Module" />
<dbType value="String" />
<size value="225" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{MODULE}"/>
</layout>
</parameter>
<parameter>
<parameterName value="#p_Component" />
<dbType value="String" />
<size value="225" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{COMPONENT}"/>
</layout>
</parameter>
</appender>
</log4net>
</configuration>
But it's not inserting them in the database. How can I get that to work?
Simple answer: Because this is an Oracle connection remove the # symbol from the parameter name and it will work.
Sample:
<parameter>
<parameterName value="#p_Module" />
<dbType value="String" />
<size value="225" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{MODULE}"/>
</layout>
</parameter>
I had similar problem, when log4net table was in different schema.
(it surely applies to stored procedure or package too if they are in different schema)
Don't forget to type your schema name before table or stored procedure then.
Sample:
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess, Version=4.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<connectionString ..../>
<commandText value="INSERT INTO schemaName.LOG4NET (LOG_DATE, LOG_LEVEL, LOG_IDENTITY, LOG_MESSAGE, LOG_EXCEPTION) VALUES (:log_date, :log_level, :log_identity, :log_message, :log_exception)" />
....
Related
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.
I made a log4net project with WinForm about keeping logs in the database. I contacted SQL Server and wrote logs here. But when I try to contact to Oracle and save log in Oracle database, it didn't work. Why? I am sure the connection string is correct, but it does not work. Is there something wrong with the connection string or is there something else I need to implement for Oracle?
Here is my Log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigrationSectionHandler,log4net"/>
</configSections>
<log4net>
<root>
<level value="All"></level>
<appender-ref ref="AdoNetAppender_Oracle"></appender-ref>
</root>
<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
<connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess,Version=2.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<connectionString value="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MYHOST)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MYDBNAME)));User Id=MYUSERID;Password=MYPASSWORD;" />
<commandText value="INSERT INTO Log (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" />
<bufferSize value="1" />
<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>
</appender>
</log4net>
</configuration>
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.
I cannot get log4net to work, and having installed Glimpse for log4net, I notice the log4net tab is greyed out, so I guess I have not installed it properly, not sure where though.
In my assemblyInfo.cs I have:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
It may not be necessary, but in Global.asax.cs I have:
var logConfigFilePath = Server.MapPath("~/log4net.config");
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(logConfigFilePath));
In my log4net.config I have:
?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<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=SQL_Mulalley;initial catalog=LogFiles;Persist Security Info=True;User ID=***;Password=****;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
<commandText value="INSERT INTO SCD4_Log4Net ([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="FATAL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
</configuration>
I obtain the logger here:
private static readonly log4net.ILog log =
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
And I call the log in my code as follows:
log.Info(string.Format("User {0} logged in.", model.UserName));
Nothing appears in my database where I have created the standard Log4Net table.
So what am I missing?
You did declare the appender but log4net is not actually using it.
As per your config file you're telling log4net to use an appender named RollingFileAppender (that is, additionally, not defined).
Change this:
<appender-ref ref="RollingFileAppender"/>
To:
<appender-ref ref="AdoNetAppender"/>
Finally pay attention to this section:
<root>
<level value="FATAL"/>
As per the line above, your (default) appender is not going to transmit any log unless you do the logs with Fatal level:
log.Fatal(string.Format("User {0} logged in.", model.UserName));
Or change the default log-level to debug:
<root>
<level value="DEBUG"/>
It can be the buffer size is 100 and hence you haven't hit the buffer limit to allow log4net to write to Db. Change it to one for testing
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
You can also see if the Adoappender is throwing any error by switching on Internal.Debug=true
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
I usually get issue with the SQL not being correct or the connection string is not correct.
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.