【Vegas原创】Log4Net实践
Windows服务程序,将所出现的问题写入log
Service1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net.Mail;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace EIPMailService
{
public partial class Service1 : ServiceBase
{
DBAccess db = new DBAccess();
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string strEX = "EX";
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
}
protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
}
void sendMail(string strID)
{
try
{
//
}
catch(Exception ex)
{
//IF repeat,do not write
if (ex.ToString() == strEX)
{
}
else
{
log.Error("Error", ex);
strEX = ex.ToString();
}
}
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//
sendMail(strID);
//
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net.Mail;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace EIPMailService
{
public partial class Service1 : ServiceBase
{
DBAccess db = new DBAccess();
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string strEX = "EX";
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
}
protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
}
void sendMail(string strID)
{
try
{
//
}
catch(Exception ex)
{
//IF repeat,do not write
if (ex.ToString() == strEX)
{
}
else
{
log.Error("Error", ex);
strEX = ex.ToString();
}
}
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//
sendMail(strID);
//
}
}
}
web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appSettings>
</appSettings>
<!-- This section contains the log4net configuration settings -->
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="F:\vegas.txt" />
<!-- Example using environment variables in params -->
<!-- <file value="${TMP}\log-file.txt" /> -->
<!--<sppendToFile value="true" />-->
<!-- An alternate output encoding can be specified -->
<!-- <encoding value="unicodeFFFE" /> -->
<layout type="log4net.Layout.PatternLayout">
<footer value="[Footer]--Test By Vegas " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" />
</layout>
<!-- Alternate layout using XML
<layout type="log4net.Layout.XMLLayout" /> -->
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<!--<target value="Console.Error" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="EventLogAppender" />
</root>
</log4net>
</configuration>
<configuration>
<configSections>
<!--<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appSettings>
</appSettings>
<!-- This section contains the log4net configuration settings -->
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="F:\vegas.txt" />
<!-- Example using environment variables in params -->
<!-- <file value="${TMP}\log-file.txt" /> -->
<!--<sppendToFile value="true" />-->
<!-- An alternate output encoding can be specified -->
<!-- <encoding value="unicodeFFFE" /> -->
<layout type="log4net.Layout.PatternLayout">
<footer value="[Footer]--Test By Vegas " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" />
</layout>
<!-- Alternate layout using XML
<layout type="log4net.Layout.XMLLayout" /> -->
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<!--<target value="Console.Error" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="EventLogAppender" />
</root>
</log4net>
</configuration>
喜欢请赞赏一下啦^_^