Hello Friends,
So manytimes we have to get the error logs comes at the time of production so at that time we don't able to find the exac error in the system. It will be tough if you try to find that bug from overall proejct. You need to have log4net.dll for this after adding reference to the project you can process for the next step. so first Download the dll file and give reference to your project.
First of all create an SQL Table for the log use following script to create table
Now Put the following Code in the <Confuguration>, Change the connection string as per your suggestion.
Next Put the following code in <configSections>
Now In Global.asax file
In the page where you want to get the error log first create the object like the following code.
and then use this code to get exceptions in the catch part
So manytimes we have to get the error logs comes at the time of production so at that time we don't able to find the exac error in the system. It will be tough if you try to find that bug from overall proejct. You need to have log4net.dll for this after adding reference to the project you can process for the next step. so first Download the dll file and give reference to your project.
First of all create an SQL Table for the log use following script to create table
CREATE TABLE [dbo].[GEN_Log](
[Id] [bigint] 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](8000) NOT NULL,
[Exception] [varchar](8000) NULL
)
[Id] [bigint] 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](8000) NOT NULL,
[Exception] [varchar](8000) NULL
)
Now Put the following Code in the <Confuguration>, Change the connection string as per your suggestion.
<log4net>
<root>
<level value="ALL" />
<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="server=10.5.4.31; uid=admin;
pwd=password; database=ProjectDB" />
<commandText value="INSERT INTO GEN_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="8000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="8000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
</log4net>
<root>
<level value="ALL" />
<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="server=10.5.4.31; uid=admin;
pwd=password; database=ProjectDB" />
<commandText value="INSERT INTO GEN_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="8000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="8000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
</log4net>
Next Put the following code in <configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
Now In Global.asax file
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
XmlConfigurator.Configure();
}
{
// Code that runs on application startup
XmlConfigurator.Configure();
}
In the page where you want to get the error log first create the object like the following code.
private static readonly ILog log = LogManager.GetLogger(typeof(PageName));
and then use this code to get exceptions in the catch part
log.Error(ex.InnerException != null ? string.Format("Page Load : Inner Exception: {0} --- Exception: {1}", ex.InnerException.Message, ex.Message) : string.Format("Page Load : {0}",ex));
No comments:
Post a Comment