I'm trying to implement log4net 2.0.8 in our dot net core 1.1.2 app. The ConsoleAppender is working fine, but the AdoNetAppender throws an error:
Could not load type 'log4net.Appender.AdoNetAppender'
Is AdoNetAppender still not supported in Dot Net Core?
My config:
<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="true">
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %-5level %logger: %message%newline" />
</layout>
</appender>
<appender name="AdoNetAppender" 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=MyMSSQLServer; initial catalog=MyDatabase;integrated security=false;persist security info=True;User ID=sa;Password=***" />
<commandText value="dbo.prcLog4Net_LogMsg" />
<commandType value="StoredProcedure" />
<parameter>
<parameterName value="#session_id" />
<dbType value="Int" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{session_id}" />
</layout>
</parameter>
<parameter>
<parameterName value="#message" />
<dbType value="AnsiString" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
The ado.net appender is not supported, you can look it up in the following page:
framework-support.html#Appenders
Related
I am using MicroKnights.Logging.AdoNetAppender for my .Net core 3.1 application.
I want rolling table name for log table (new table for every month's logs).
Is there any possibilities to achieve this by making any changes in log4net.config file?
My log4net.config looks like below
<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="true">
<appender name="DebugAppender" type="log4net.Appender.DebugAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Log\logs_" />
<staticLogFileName value="false" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd'.log'" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" />
</layout>
</appender>
<appender name="AdoNetAppender" type="MicroKnights.Logging.AdoNetAppender, MicroKnights.Log4NetAdoNetAppender">
<bufferSize value="1" />
<connectionType value="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient, Version=1.0.0.0,Culture=neutral,PublicKeyToken=23ec7fc2d6eaa4a5"/>
<connectionStringName value="log4net" />
<connectionStringFile value="appsettings.json" />
<commandText value="INSERT INTO [Log_#TableName]
([ApiName]
,[RequestTimeUtc]
,[TimeElapsed]
,[Headers]
,[Body]
,[QueryString])
VALUES
(#ApiName
,#RequestTimeUtc
,#TimeElapsed
,#Headers
,#Body
,#QueryString)" />
<parameter>
<parameterName value="#TableName" />
<dbType value="String" />
<size value="10000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{TableName}" />
</layout>
</parameter>
<parameter>
<parameterName value="#ApiName" />
<dbType value="String" />
<size value="10000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{ApiName}" />
</layout>
</parameter>
<parameter>
<parameterName value="#RequestTimeUtc" />
<dbType value="DateTime" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{RequestTimeUtc}" />
</layout>
</parameter>
<parameter>
<parameterName value="#TimeElapsed" />
<dbType value="String" />
<size value="10000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{TimeElapsed}" />
</layout>
</parameter>
<parameter>
<parameterName value="#Headers" />
<dbType value="String" />
<size value="10000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Headers}" />
</layout>
</parameter>
<parameter>
<parameterName value="#Body" />
<dbType value="String" />
<size value="10000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Body}" />
</layout>
</parameter>
<parameter>
<parameterName value="#QueryString" />
<dbType value="String" />
<size value="10000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{QueryString}" />
</layout>
</parameter>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="DebugAppender" />
<appender-ref ref="RollingFile" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
Executing Stored Procedure looks last option to me.
Modify the config with
<commandText value="INSERT INTO {0}
([ApiName]
,[RequestTimeUtc]
,[TimeElapsed]
,[Headers]
,[Body]
,[QueryString])
VALUES
(#ApiName
,#RequestTimeUtc
,#TimeElapsed
,#Headers
,#Body
,#QueryString)" />
And init log4net as follow:
public IEnumerable<string> InitializeLog4Net()
{
var repository = LogManager.GetRepository(GetType().Assembly);
ConnectionString = _configuration.GetConnectionString("DefaultConnection");
if (string.IsNullOrWhiteSpace(ConnectionString))
throw new NullReferenceException($"Did not find the connectionString");
var log4NetConfigFilename = $"Logging\\log4net.config";
var fileInfo = new FileInfo(log4NetConfigFilename);
if (fileInfo.Exists == false)
throw new FileNotFoundException($"Did not find log4net configuration file \"{log4NetConfigFilename}\"");
XmlConfigurator.Configure(repository, fileInfo);
if (repository.Configured == false)
throw new InvalidOperationException("Repository not configured");
if (repository.ConfigurationMessages.Count != 0)
throw new InvalidOperationException($"Repository messages found: {string.Join(",", repository.ConfigurationMessages)}");
foreach (var appender in repository.GetAppenders())
{
if (appender is AdoNetAppender adoNetAppender)
{
adoNetAppender.CommandText = string.Format(adoNetAppender.CommandText, TableName);
adoNetAppender.ConnectionString = ConnectionString;
adoNetAppender.ErrorHandler = this;
adoNetAppender.ActivateOptions();
}
}
return _log4netErrors;
}
Generate problematically the TableName or take it from appsettings.json
I'm using Entity Framework code-first, and trying to get log4Net to log to my SQL Server database. I have my buffer size value set to 1, and added
log4net.Config.XmlConfigurator.Configure();
to my Global.asax file.
I've also added
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
However, there's still nothing outputting to my SQL Server database. I have my Entity Framework stuff in a separate project with an ErrorLog data model in there. My log4Net settings are in the web.config for my web project, and the web project has a reference to my Entity Framework project.
Here's what I have in my web.config for log4Net
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<threshold value="ALL" />
<connectionType value="System.Data.EntityClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=(LocalDb)\MSSQLLocalDB;initial catalog=AutoForms;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" />
<commandText value="INSERT INTO ErrorLog ([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>
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
I'm not getting any errors. Not sure what else to do. I've tried added those log4Net settings in the app.config of my project that handles all of the Entity Framework stuff with no success. Any help would be appreciated. Thanks.
I was able to figure this out after digging around for a bit. My value for connection type was
<connectionType value="System.Data.EntityClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
instead of
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
Notice the connectionType value. It worked after changing this. Hope it helps someone.
You have to set context.Database.Log with a delegate to EF output any thing from your DB.
if for one use nLog , for example :
context.Database.Log = s=> LogManager.GetLogger("myLogger").Info(s);
Don't know how it would be with log4net but it's something similar
I have a web service set up in my IIS, the appender that logs to a text file is working fine (the text file is in the same directory as where the web service is running from). The appender that writes to the Event Viewer isn't working, I'm currently connected to the web service as administrator so I should in theory be able to do this, what else should I check/see if I'm missing?
log4net.config:
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<param name="ApplicationName" value="Lending Service" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message %newline %exception" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="DEBUG"/>
<file value="webLog.log"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="EventLogAppender" />
</root>
<logger name="LendingService.Global_asax">
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="EventLogAppender" />
</logger>
<logger name="LendingService.LendingService">
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
The best way to find out what is going wrong is enable log4net interal debug, this will tell you where log4net is failing:
<configuration>
...
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Log4net FAQ
i was thinking to use Log4Net for our webform application in asp.net. so i checked few article on this but i saw all article not saying about each property regarding configuration.
so i apologized that i have to paste bit big config section here and looking for some one who can help me to understand each property usage.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="DbAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="0" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="{auto}" />
<commandText value="INSERT INTO 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="%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="#logger" />
<dbType value="String" />
<size value="1000" />
<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>
<root>
<level value="INFO" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="DbAppender" />
</root>
</log4net>
</configuration>
please see the above config details and answer each property.
1) why <bufferSize value="0" /> is set to 0 ? when one should set other value to bufferSize property ?
2) <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
how do i know what PublicKeyToken value i need to use.
3) what is the meaning of connection string auto <connectionString value="{auto}" /> ?
4)
what is the meaning of conversionPattern or
5 what is ExceptionLayout <layout type="log4net.Layout.ExceptionLayout" />
6) what is the meaning of below xml
<root>
<level value="INFO" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="DbAppender" />
</root>
what is RollingFileAppender ?
what is DbAppender ?
please discuss about my each point & question. thanks
1/ bufferSize <= 1 means no buffering. Log is written instantly to the database.
http://logging.apache.org/log4net/release/sdk/log4net.Appender.BufferingAppenderSkeleton.BufferSize.html
2/ PublicKeyToken is not Log4net related. Have a look. Keep the given token.
3/ {auto} in connectionString seems to be custom code, maybe taken from here
4/ List of conversion patterns
5/ Exception layout: only the exception text from the logging event will be logged.
http://logging.apache.org/log4net/release/sdk/log4net.Layout.ExceptionLayout.html
6/
FileAppender -> log to file.
RollingFileAppender -> log to file with rotating logs (max N files of length L, then they are erased).
DbAppender -> log to database, that's why there is a connectionString.
More info: http://logging.apache.org/log4net/release/config-examples.html
I want to make Log4Net to enter logs in MS Sql Server 2005. I am using .NET 2.0.
Here is the code till now I have written:
Web.Config File
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<connectionStrings/>
<log4net>
<appender name="AdoNetAppender" 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=XYZ;Initial Catalog=ACC;Integrated Security=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>
</log4net>
Global.asax
<%# Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
log4net.Config.XmlConfigurator.Configure();
}
Page_load
try
{
// There is no such Session, this is just to create error
if (Session["userName"].ToString() == "Admin")
{
}
}
catch(Exception ex)
{
log4net.ILog log =log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log4net.Config.XmlConfigurator.Configure();
log.Debug("log Debug",ex);
log.Info("log Info",ex);
log.Warn("log Warn",ex);
log.Error("log Error",ex);
log.Fatal("log Fatal",ex);
}
Actually I have tried it by storing lod details in Log folder in my directory, which was working fine. But I want to store information in Sql Server 2005.
Am I missing something? or I have to correct some where.
Please help.
Thanks in advance
Unless I made some mistake when I edited your question, then the problem is that you do not reference the appender like this:
<root>
<level value="ALL" />
<appender-ref ref="AdoNetAppender" />
</root>
Some further comments:
You do not need to call the Configure method twice
You do not need an exception to test logging