I have written WCF REST Service. Now I need to enable logging. I am using log4net but nothing is logging in my database.
I have gone through following links but all in vain.
How to configure a child logger with a lower loglevel than the root level in Log4net
Log4Net in WCF not working
Log4net Issue inside wcf
Log4Net with WCF Running As Service
I am using ADONetAppender. Here is my log4net.config file.
<appender name="ADONetAppenderMessageLog" type="log4net.Appender.ADONetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=FAIZAN;Initial Catalog=AnasageLogging;Integrated Security=SSPI;" />
<!-- <commandText value="INSERT INTO MessageLog ([TransactionID],[SessionID],[UserID],[Message],[MessageType],[CreatedDate],[CreatedBy],[ApplicationID]) VALUES (#transactionId, #sessionId, #userId, #message, #messageType, #createdTime,#createdBy,#applicationId)" />-->
<commandText value="spMessageLogInsert" />
<commandType value="StoredProcedure"/>
<parameter>
<parameterName value="#transactionId"/>
<dbType value="String"/>
<size value="80"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Transaction_ID}"/>
</layout>
</parameter>
<parameter>
<parameterName value="#sessionId"/>
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Session_ID}"/>
</layout>
</parameter>
<parameter>
<parameterName value="#userId"/>
<dbType value="String"/>
<size value="80"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{User_ID}"/>
</layout>
</parameter>
<parameter>
<parameterName value="#message"/>
<dbType value="String"/>
<size value="10000"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Message}"/>
</layout>
</parameter>
<parameter>
<parameterName value="#messageType"/>
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Message_Type}"/>
</layout>
</parameter>
<parameter>
<parameterName value="#createdTime"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
<parameter>
<parameterName value="#createdBy"/>
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Created_By}"/>
</layout>
</parameter>
<parameter>
<parameterName value="#applicationId"/>
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Application_ID}"/>
</layout>
</parameter>
</appender>
Here is how I am using it.
static log4net.Util.LogicalThreadContextProperties properties = log4net.LogicalThreadContext.Properties;
private static log4net.ILog log { get; set; }
public static void Write( object session_id, object user_id, object message, object message_type, object created_by, object msg)
{
properties["Session_ID"] = session_id;
properties["User_ID"] = user_id;
properties["Message"] = message;
properties["Message_Type"] = message_type;
properties["Created_By"] = created_by;
log.Info(msg);
}
NOTE: This is my wrapper class function.
Every thing is working fine, No exception is thrown. But when I see my database table, it is empty. I am testing it on my local machine not on production. I don't know what I am missing. Any help will be appreciated.
Thanks
Related
I have seen multiple links on how to do this and my version is techically working.
Example: Example
So, I have added a new property, Unit_Name, to my code.
for (int i = 0; i < 5; i++)
{
log4net.GlobalContext.Properties["Unit_Name"] = "Bob " + i.ToString();
log.Fatal("This is fatal");
}
And in the config file for the Console Appender
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger %property{unit_name} - %message %newline" />
</layout>
</appender>
This works, so moving forwards I added this to my AdoNetAdapter section.
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[UnitName],[Message],[Exception]) VALUES (#log_date, #thread, #log_level, #logger,#unit_name, #message, #exception)" />
and a parameter section
<parameter>
<parameterName value="#unit_name" />
<dbType value="String" />
<size value="200" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property(Unit_Name)" />
</layout>
</parameter>
Now on testing all is still working, but the above parameter should be displaying the string Bob 1, Bob 2, etc. But on looking at the database table, I am getting the following:
{log4net:HostName=GBRCL33199, log4net:Identity=, log4net:UserName=userName, Unit_Name=Bob 0}(Unit_Name)
This must be a config error but I cannot see where or what I have done wrong.
Thank for those that looked at this.
I knew it must have been something I did. As it happens, changing the conversionPattern string below
<parameter>
<parameterName value="#unit_name" />
<dbType value="String" />
<size value="200" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property(Unit_Name)" />
</layout>
</parameter>
to
<parameter>
<parameterName value="#unit_name" />
<dbType value="String" />
<size value="200" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Unit_Name}" />
</layout>
</parameter>
fixed the problem. Or if you still cannot see it. I changed "%property(Unit_Name)" to "%property{Unit_Name}".
im trying to enter every 100 records into the database.If i specify the buffer size as 100.It stills enters each record into the database.Is there a way where i can specify the flush interval and buffer size.So which comes first it takes that.
This is my config.
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="10" />
<connectionType value="System.Data.SqlClient.SqlConnection,
System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=;User ID=;Password=" />
<commandText value="INSERT INTO Errorlog ([clientname],[username],[administration],[selectedentity],[project],[Date],[Thread],[Level],[Logger],
[Message],[Exception],[browser],[ipaddress],[url],[errormessage]) VALUES (#clientname,#username,#administration,#selectedentity,#project,#log_date, #thread, #log_level,
#logger, #message, #exception,#browser,#ipaddress,#url,#errormessage)"/>
<parameter>
<parameterName value="#clientname" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{clientname}" />
</layout>
</parameter>
<parameter>
<parameterName value="#username" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{username}" />
</layout>
</parameter>
<parameter>
<parameterName value="#administration" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{administration}" />
</layout>
</parameter>
<parameter>
<parameterName value="#selectedentity" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{selectedentity}" />
</layout>
</parameter>
<parameter>
<parameterName value="#project" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{project}" />
</layout>
</parameter>
<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>
<parameter>
<parameterName value="#browser" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{browser}" />
</layout>
</parameter>
<parameter>
<parameterName value="#ipaddress" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{ipaddress}" />
</layout>
</parameter>
<parameter>
<parameterName value="#url" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{url}" />
</layout>
</parameter>
<parameter>
<parameterName value="#errormessage" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{errormessage}" />
</layout>
</parameter>
</appender>
<root>
<appender-ref ref="AdoNetAppender"/>
</root>
</log4net>
Buffersize only specifies how many items shall be kept in RAM before committing them. It does not define the Lossyness of the logger.
To define a lossy logger, you need to state that explicitly and define what can be lost:
<lossy value="true"/>
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR" />
</evaluator>
I am using log4net for my c# code to insert log into database using the configuration,
<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=[database server];
initial catalog=[database name];integrated security=false;
persist security info=True;User ID=[user];Password=[password]" />
<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>
So i just need to use their inbuilt functions for inserting data to table. Now i need to retrieve data from the log table. Is log4net providing any functions for retrieving data from table? Or the only way to retrive data from this table is writing my own query? Anyone please help me.
I haven't tried it, but a simple Google search got me to http://code.google.com/p/log4net-db-viewer/ and some other results.
It does not look like there is any built in reader for any appender.
Log4net does not give you any tools to read your logs. Its only for making logs and it depends on the appenders to format and save the logs somewhere. There are many projects and samples which can read your logs depending on your appender:
read log4net logs
I have a windows service set up with log4net, logging to a file and a SQL Server database.
During initilization, I do my XmlConfigurator.Configure() call. Early on in the service, I execute some logging. It's a windows service, so it's starting up around the same time that SQLServer starts up. My service is doing logging before the DB is up. When the DB is up, log4net has given up logging to the DB and it fails to proceed.
I'd like for log4net ADONetAppender to start appending to the log database as soon as the database is online. Is this possible? Can I just call XmlConfigurator.Configure() when the DB is back online?
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="C:\Logs\MyLog.txt" />
<param name="AppendToFile" value="true" />
<param name="ImmediateFlush" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]
" />
<param name="Footer" value="[Footer]
" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</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=localhost\SQLEXPRESS;Initial Catalog=tests_db;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.RawUtcTimeStampLayout"/>
</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="512"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p"/>
</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="2000"/>
<layout type="log4net.Layout.ExceptionLayout"/>
</parameter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ADONetAppender"/>
</root>
</log4net>
I tried using log4net for logging info into db and I stuck with it now.Not sure what I am doing wrong.Whatever message i send its not received in the database,neither shows any error
I have a C# basic WINForm application and I am trying to insert log into db.
My log4net.xml below.
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="ADONetAppender" />
</root>
<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=CTI-OPT780-06\MSSQL2008; Initial Catalog=MYDB; 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>
<parameter>
<parameterName value="#context" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%x" />
</layout>
</parameter>
</appender>
</log4net>
C# code to access it :
private ILog logger = LogManager.GetLogger(typeof(DBAccess));
public TestDB()
{
logger.Debug("Here is a debug log.");
logger.Info("... and an Info log.");
logger.Warn("... and a warning.");
logger.Error("... and an error.");
logger.Fatal("... and a fatal error.");
}
Create table for Log
CREATE TABLE [dbo].[Log]
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[Date] [datetime] NOT NULL,
[Thread] [varchar](32) NOT NULL,
[Context] [varchar](512) NULL,
[Level] [varchar](512) NOT NULL,
[Logger] [varchar](512) NOT NULL,
[Message] [varchar](4000) NOT NULL,
[Exception] [varchar](2000) NULL
)
It would really great if someone points me where I m making mistake . Thanks
thanks for your wonderful help .Its so stupid of me I didnt do "configure" .
Added
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(sFIleName + "\\log4net.xml"));
and everything got fixed..
Using the debug trace as told by #sgmoore also helped to fix the db access rights issue.