1、using System;using System.Configuration;using System.Data;using log4net;using log4net.Config;using log4net.Appender;using log4net.Layout;using log4net.Util;namespace BizTalk.DAL.Log/ / 日志文件处理/ public sealed class LogUtil#region 变量/ConfigurationSettings.AppSettingsprotected static readonly log4net.IL
2、og logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);private static string logFilePath = string.Empty;/日志文件 错误文件 处理文件路径private static string str_configfile = “E:Suzhou Project EDI代码BizTalkSolution_suzhouBizTalk.suzhou.ConfigConfigDB.config“;/日志文件的读写也由 xml 文
3、件读取private static string logErrFilePath =System.Configuration.ConfigurationManager.AppSettings“ErrFilePath“;private static string logWarnFilePath = string.Empty;/日志文件路径private static string logInfoFilePath =System.Configuration.ConfigurationManager.AppSettings“logFilePath“;#endregion/ / Ctr/ public
4、LogUtil() #region 获得参数设置 / / 根据配置文件里面参数名称读参数/ / / public static string Get_Parameter(string ParameterName) DataSet ds_config =BizTalk.DAL.Data.XmlObject.GetXml(str_configfile);string str_xml_proc = ds_config.TablesParameterName.Rows0“value“.ToString();return str_xml_proc;#endregion#region 写日志文件 Meth
5、odpublic static void WriteLog(string msg)string filePath = logFilePath;FileAppender fa = GetFileAppender(filePath);log4net.Config.BasicConfigurator.Configure(fa);/写入日志logger.Debug(msg);/日志文件写入public static void WriteLogInfo(string msg)/string filePath = logInfoFilePath;string filePath = Get_Paramete
6、r(“logFilePath“).Trim();FileAppender fa = GetFileAppender(filePath);log4net.Config.BasicConfigurator.Configure(fa);logger.Info(msg); public static void WriteLogWarn(string msg)string filePath = logWarnFilePath;FileAppender fa = GetFileAppender(filePath);log4net.Config.BasicConfigurator.Configure(fa)
7、;logger.Warn(msg);/错误文件写入public static void WriteLogErr(string msg)/string filePath = logErrFilePath;string filePath = Get_Parameter(“ErrFilePath“).Trim();FileAppender fa = GetFileAppender(filePath);log4net.Config.BasicConfigurator.Configure(fa);logger.Error(msg);#endregion/ / 取得日志文件位置和样式/ / 文件路径/ p
8、rivate static FileAppender GetFileAppender(string filePath)/日志文件布局PatternLayout sLayout = new PatternLayout(“%date %thread %-5level %logger %ndc - %message%newline“);sLayout.Header = “Headerrn“;sLayout.Footer = “Footerrn“;/日志文件路径string fileName = filePath + “ + System.DateTime.Now.ToString(“yyyy-MM-
9、dd“)+ “ + System.DateTime.Now.ToString(“yyyy-MM-dd“) + “-Log.log“;FileAppender fa = new FileAppender(sLayout, fileName);return fa;复制代码using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;namespace BizTalk.DAL.Logpublic class LogBaseprivate static void Write(string
10、 msg, string category)string path = string.Format(“c:0.log“, category);using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path, true)trystring lin = string.Format(“2 0rn:1“, category, msg, DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss“);sw.WriteLine(lin);sw.Close();catch (Exception e)Console
11、.WriteLine(e);sw.Close();public static void TraceWrite(string message, string category)tryif (category = “I“)TraceSwitch mySwitch = new TraceSwitch(“BizImpTrace“, “Biztalk trace message“);if (mySwitch.TraceError)Write(message, “BizImpException“);/Trace.Listeners1.WriteLine(message, “BizImpException“
12、);else if (category = “E“)TraceSwitch mySwitch = new TraceSwitch(“BizExTrace“, “Biztalk trace message“);if (mySwitch.TraceError)/System.Diagnostics.DefaultTraceListener = Trace.Listeners2;Write(message, “BizExException“);/Trace.Listeners2.WriteLine(message, “BizImpException“);else if (category = “R“
13、)TraceSwitch mySwitch = new TraceSwitch(“BizRouterTrace“, “Biztalk trace message“);if (mySwitch.TraceError)/System.Diagnostics.DefaultTraceListener = Trace.Listeners2;Write(message, “BizRouterException“);/Trace.Listeners3.WriteLine(message, “BizRouterException“);else/if (mySwitch.TraceError)/System.Diagnostics.DefaultTraceListener = Trace.Listeners2;Write(message, “BizException“);/Trace.Listeners0.WriteLine(message, “BizRouterException“);/catch 复制代码配置文件增加:复制代码在指定的目录,按照日期生成日志文件