1.使用winform形式
①。AssemblyInfo.cs中增加[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
②。引用log4net Log4Net.config
③。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
namespace WindowsFormsSolaceTest
{
class loghelp
{
//第四步:在项目文件根目录(其实无所谓,想建在哪里都是阔以滴)建一个LogHelper.cs类
public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
public static void WriteLog(string info)
{
if (loginfo.IsInfoEnabled)
{
loginfo.Info(info);
}
}
public static void WriteLog(string info, Exception se)
{
if (logerror.IsErrorEnabled)
{
logerror.Error(info, se);
}
}
}
}
④。loghelp.WriteLog("Error: " + args.Event.ToString());
服务中的使用
1.服务的 appcongfig中加入
<configSections>
<section name ="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SolaceMessageServer.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
②。引用log4net Log4Net.config
③。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
namespace SolaceMessageServer
{
class LogHelper
{
public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
public static readonly ILog _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void WriteLog(string info)
{
if (loginfo.IsInfoEnabled)
{
loginfo.Info(info);
}
}
public static void WriteLog(string info, Exception se)
{
if (logerror.IsErrorEnabled)
{
logerror.Error(info, se);
}
}
}
}
④。loghelp.WriteLog("Error: " + args.Event.ToString());