I have a problem with Log4Net. It doesn't add any entry in the table...
here is the table query
CREATE TABLE [dbo].[Log4Net_Error](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Date] [datetime] NOT NULL,
[Thread] [varchar](255) NOT NULL,
[Level] [varchar](50) NOT NULL,
[Logger] [varchar](255) NOT NULL,
[Message] [varchar](4000) NOT NULL,
[Exception] [varchar](2000) NULL
) ON [PRIMARY]
GO
and the code in the web.config is bellow:
<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=.\SQLEXPRESS;Initial Catalog=pnawebloket;Integrated Security=True" />
<commandText value="INSERT INTO Log4Net_Error2 ([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>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="AdoNetAppender" />
</root>
Also, in global.asax.cs in Aplication_Start i added the line
log4net.Config.XmlConfigurator.Configure();
And i still have no entry in the table.
public ActionResult Employees()
{
log4net.ILog logger = log4net.LogManager.GetLogger(this.GetType());
logger.Info("test");
//throw new Exception("ex");
ViewBag.Message = "Bellow is a table with employee's";
var employees = employeerepository.FindAllEmployee().ToList();
return View(employees);
}
If i try to add the message in a text file work's fine.
App is in MVC3 and i'm using sql server 2008r2
Any suggestions??
Your Log table : Log4Net_Error ; Log Table at configuration file : Log4Net_Error2. You should change table name as Log4Net_Error at configuration file.
Related
I have been using log4net to log to an Oracle database for the past few weeks and everything was working until I was told that Native Network Encryption had been turn on. This caused my logging to cease and I'm clueless on how to fix it. No errors are given, it just won't write to the DB. I am using the following logger.xml file:
<log4net>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<immediateFlush value="true" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
<connectionType value="Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<connectionStringName value="Payments" />
<commandText value="INSERT INTO CROWN.EOB_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>
<root>
<level value="ALL" />
<appender-ref ref="AdoNetAppender_Oracle" />
</root>
</log4net>
As I said this was working perfectly until NNE was turned on. The application I am using is a C# console app. Can anyone point me in the right direction?
I am currently using Log4net to log in a table but I need to stop using that table and log everything in another table for logging errors. Basically I just changed the log4net values and parameters and the table in which I am logging. Here is the Log file
<!--SQL LOG TABLE Appender-->
<appender name="LogAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<!--THIS SETTING ALTERS HOW OFTEN IT WRITES ALL THE LOGGING TO THE DB-->
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<!--local logging connection string-->
<connectionStringName value="DSFDefaultConnection" />
<commandText value="INSERT INTO ProviderHistoryLog ([LogDate],[Event], [CalledPackageName],[ActivityDescription],[SubPackageTaskName],[TaskName]) VALUES (#log_date, #thread, #logger, #message, #notApplicable, #requesturi)" />
<parameter>
<parameterName value="#ProviderId" />
<dbType value="long" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="null" />
</layout>
</parameter>
<parameter>
<parameterName value="#log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawUtcTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="#thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="#logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger | %class " />
</layout>
</parameter>
<!--<parameter>
<parameterName value="#identity" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="DPPAdmin#aez.com"/>-->
<!-- <converter>-->
<!-- <name value="httpuser" />-->
<!-- <type value="fi360.DataServiceFramework.Common.Logging.HttpIdentityUserPatternConverter" />-->
<!-- </converter>-->
<!-- </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="#notApplicable" />
<dbType value="String" />
<size value="-1" />
<layout type="%notApplicable" />
</parameter>
<parameter>
<parameterName value="#requesturi" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%requesturi"/>
<converter>
<name value="requesturi" />
<type value="fi360.DataServiceFramework.Common.Logging.RequestUriUserPatternConverter" />
</converter>
</layout>
</parameter>
</appender>
The problem is, it's not logging in the ProviderHistoryLog table. It's not logging anything. As you can see the value for the buffer size is 1 so I am thinking the issue is in the INSERT INTO statement.
I have a problem with the AdoNetAppender of log4net in my ASP.NET Core application (netcoreapp1.1).
If I want to use the AdoNetAppender, I get the following error:
System.TypeLoadException: Could not load type 'log4net.Appender.AdoNetAppender' from assembly 'log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'.
Is there anybody who have the AdoNetAppender running under ASP.NET Core (netcoreapp1.1)? If not, is there a good alternative to log with log4net into a database? I also use Entity Framework Core.
The AdoNetAppender isn't supported at present on .NET Core 1.0 / .NET Standard 1.3: https://logging.apache.org/log4net/release/framework-support.html#Appenders
For .Net Core you can use nuget.org/packages/MicroKnights.Log4NetAdoNetAppender . It has a bit differences in set up compare to AdoNetAppender, but is very straightforward too, to Find how to set it up here: github.com/microknights/Log4NetAdoNetAppender
Here an example that works for me
<appender name="AdoNetAppender" type="MicroKnights.Logging.AdoNetAppender, MicroKnights.Log4NetAdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection,System.Data.SqlClient,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" />
<connectionStringName value="log4net" />
<connectionStringFile value="appsettings.json" />
<commandText value="INSERT INTO Log ....." />
<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>
and in your appsettings.json
"ConnectionStrings": {
"log4net": "Data Source=.\\SQLEXPRESS;Initial Catalog=LogDB;User ID=user;Password=pass",
}
I want use Log4Net in my application for logging to console and database(Oracle)
I made this setting :
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%thread] - Level%newline - %level%newlineMessage - %message%newlineUTC Date - %utcdate{ABSOLUTE}%newlineDate - %date{ABSOLUTE}%newline%newline "/>
</layout>
</appender>
<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
<connectionType value="Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
<connectionString value="DATA SOURCE= (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=myIpAddress)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xe))); USER ID=user_name PASSWORD=my_password;"/>
<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>
<root>
<level value="All"/>
<appender-ref ref="AdoNetAppender_Oracle"/>
<appender-ref ref="ConsoleAppender"/>
</root>
On console I have output, but after start there is some Exception thrown: 'Oracle.ManagedDataAccess.Client.OracleException' in Oracle.ManagedDataAccess.dll
And of course there is no new line in Oracle table.
Any suggestion why not working ?
Thank you.
This is a working configuration for my custom table but you can use the default columns. Pay attention on which OracleDataAccess use in your application (Version=2.121.2.0) Check here AdoNetAppender.ConnectionType Property
<appender name="DatabaseAppender" type="log4net.Appender.AdoNetAppender">
<connectionString value="Data Source=HR;User Id=HRL;Password=HR;Max Pool Size=30" />
<connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess, Version=2.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<commandText value="INSERT INTO HRPORTAL.LOG_WEB (DATA,SESSIONE,ENTITA,DESCRIZIONE,DETTAGLIO,MODULO) VALUES (:data, :sessione,:entita,:descrizione, :dettaglio,:modulo)" />
<bufferSize value="1" />
<parameter>
<parameterName value=":data" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value=":sessione" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{sessione}" />
</layout>
</parameter>
<parameter>
<parameterName value=":entita" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{entita}" />
</layout>
</parameter>
<parameter>
<parameterName value=":descrizione" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{descrizione}" />
</layout>
</parameter>
<parameter>
<parameterName value=":dettaglio" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{dettaglio}" />
</layout>
</parameter>
<parameter>
<parameterName value=":modulo" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{modulo}" />
</layout>
</parameter>
</appender>
<root >
<level value="ALL" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
I found similar questions, but non of the answers worked.
Log4Net doesn't write anything in the log database and I don't really know how to find the error.
This is my config file:
<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=sqlServer;initial catalog=Logging;integrated security=false;persist security info=True;User ID=logUser;Password=logPassword" />
<commandText value="INSERT INTO dbo.myLogTable ([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>
It's actually a complete copy of the SQL Server example on the apache page. I've only changed the connection string. The connection itself should work fine, I've tested the connection string with a SqlConnection.
This is how I create the logging object and load the config:
public static class LogFactory
{
public const string Log4NetConfig = "log4net.config";
public static ILog GetLogger()
{
var uri = new Uri(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase), Log4NetConfig));
var configFile = new FileInfo(Path.GetFullPath(uri.LocalPath));
XmlConfigurator.ConfigureAndWatch(configFile);
ILog log = LogManager.GetLogger(typeof(LogFactory));
return log;
}
}
Logging itself:
private static readonly ILog Log = LogFactory.GetLogger();
[...]
Log.Info("Test 123");
Logging works fine if I replace the config with my usual "log to file" config, so I guess the error has to be somewhere in the config, but the config is the default example from the homepage.
Can you see an error somewhere?
you did not define when the appender should be used and also with bufferSize you postponed writing to db.
<log4net>
<root>
<level value="ALL" /> <!--send all logs to appenders-->
<appender-ref ref="AdoNetAppender" />
</root>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" /> <!--THIS WILL POSTPONE DB WRITING UNTIL A BUFFER OF 100 LOG IS REACHED-->
<threshold value="INFO"/> <!--log when >= INFO -->
.....
</appender>
</log4net>
Final thoughts:
You are calling the log4net configurator every time you need a logger and this is not how you should do it.
It should be called only the first time when you start your app (static Main method) or website (global.asax applicationStart).
Then your classes should have the readonly ILog property:
private static ILog logger = LogManager.GetLogger(typeof(MyClass));
<bufferSize value="100" />
This mean that Log4Net will keep 100 records in memory before inserting into the database.
This is by design to limit round trip to the database. Try setting <bufferSize value="1" /> or maybe 0 could work.
While using AdoNetAppender, i am unable to write the log details into the database.
Following are the changes that has been done to make it a workable solution :
1) Use bufferSize value="1" or bufferSize value="0"
2) 'root' directory should be used before all the Appenders that has been used
Eg.
<root>
<level value="ALL"></level>
<appender-ref ref="AdoNetAppender"></appender-ref>
<appender-ref ref="RollingFileAppender"></appender-ref>
</root>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="mylogfile.txt" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<connectionString value="data source=server_name; initial catalog=database_name;integrated security=false; persist security info=True;User ID=user_id;Password=password" />
<commandText value="INSERT INTO ErrorLog ([Logger]) VALUES (#logger)" />
<parameter>
<parameterName value="#logger" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
</appender>
</log4net>