咱們在作開發的時候,須要把一些信息記錄下來,方便問題排查、數據分析和統計。一般咱們使用log4net做爲logging的工具,可是大部分時候須要加以封裝,以便更加方便的使用,而且不妨礙主業務程序的運行。下面就是一個異步logging的例子,關鍵在於:php
首先,在一個新建的工程裏引用log4net.dll,而且進行簡單的封裝。
html
using System;
using System.Diagnostics;
using System.IO;
using log4net;
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
namespace Log
{
public partial class CommonLogging
{
private const string StringNewLineString = "-------------------------------------------------------";
private static ILog log;
static CommonLogging() {
log = LogManager.GetLogger("common");
StartLogThread();
}
public static void Info(string message) {
AddLog(message);
}
public static void Info(string message, Exception ex) {
AddLog(message, LogType.Info, ex);
}
public static void InfoLine(string message) {
AddLogFormat("{0}\r\n{1}", LogType.Info, null, StringNewLineString, message);
}
public static void Warn(string message) {
AddLog(message, LogType.Warn);
}
public static void Warn(string message, Exception ex) {
AddLog(message, LogType.Warn, ex);
}
public static void Debug(string message) {
AddLog(message, LogType.Debug);
}
public static void Debug(string message, Exception ex) {
AddLog(message, LogType.Debug, ex);
}
public static void Error(string message) {
AddLog(message, LogType.Error);
}
public static void Error(string message, Exception ex) {
if (null == ex)
{
Error(message);
return;
}
AddLog(message, LogType.Error, ex);
}
public static void Fatal(string message) {
AddLog(message, LogType.Fatal);
}
public static void Fatal(string message, Exception ex) {
AddLog(message, LogType.Fatal, ex);
}
public static void InfoFormat(string format, params string[] args) {
AddLogFormat(format, LogType.Info, null, args);
}
public static void ErrorFormat(string format, params string[] args) {
AddLogFormat(format, LogType.Error, null, args);
}
public static void ErrorFormat(string format, Exception ex, params string[] args) {
AddLogFormat(format, LogType.Error, ex, args);
}
public static void WatchToInfoLog(string message, Action action) {
Stopwatch sw = Stopwatch.StartNew();
Info(string.Format("start to {0}", message));
action();
sw.Stop();
Info(string.Format("{0} completed..., cost: {1}", message, sw.Elapsed.TotalSeconds));
}
public static bool CatchLog(Action action, string errorMsg, bool isThrowException = false) {
if (null == action)
{
return true;
}
try
{
action();
return true;
}
catch (Exception ex)
{
Error(errorMsg, ex);
if (isThrowException)
{
throw;
}
return false;
}
}
private static string GetLogFileName(string tname) {
string name;
string basedir = AppDomain.CurrentDomain.BaseDirectory;
int pos = basedir.IndexOf("\\inetpub\\");
if (pos < 0)
{
// we are not running under an inetpub dir, log underneath the base dir
string separator = basedir.EndsWith("\\") ? null : "\\";
name = AppDomain.CurrentDomain.BaseDirectory + separator + @"logs\" + "nevmiss" + tname + ".log";
}
else
{
// we're running on an IIS server, so log under the logs directory so we can share it
name = basedir.Substring(0, pos + 9) + "logs" + Path.DirectorySeparatorChar + "nevmiss_" + tname + ".log";
}
return name;
}
}
}複製代碼
使用一個partial類來進行擴展:
web
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace Log
{
public partial class CommonLogging
{
private static ConcurrentQueue<LoggingModel> messageQueue;
private static Thread thread;
private static void StartLogThread() {
messageQueue = new ConcurrentQueue<LoggingModel>();
thread = new Thread(InternalWriteLog);
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
}
private static void AddLog(string message, LogType type = LogType.Info, Exception ex = null) {
messageQueue.Enqueue(new LoggingModel(message, type, ex));
CommonLogging.Trigger();
}
private static void AddLogFormat(string format, string arg0, LogType type = LogType.Info, Exception ex = null) {
try
{
messageQueue.Enqueue(new LoggingModel(string.Format(format, arg0), type, ex));
CommonLogging.Trigger();
}
catch (Exception exception)
{
AddLog(string.Format("Add Log Format error, format string:'{0}' , arg0:{1}.", format, arg0), LogType.Error, exception);
}
}
private static void AddLogFormat(string format, LogType type = LogType.Info, Exception ex = null, params string[] args) {
try
{
messageQueue.Enqueue(new LoggingModel(string.Format(format, args), type, ex));
CommonLogging.Trigger();
}
catch (Exception exception)
{
AddLog(
string.Format("Add Log Format error,format:'{0}', arg:{1}.", format, null == args ? null : string.Join(" , ", args)),
LogType.Error,
exception);
}
}
public static void Trigger() {
if (IsProcessing)
{
return;
}
else
{
Task.Factory.StartNew(() =>
{
InternalWriteLog();
});
}
}
private volatile static bool IsProcessing = false;
public static void InternalWriteLog() {
LoggingModel model;
while (messageQueue.TryDequeue(out model))
{
IsProcessing = true;
switch (model.MessageType)
{
case LogType.Info:
{
log.Info(model.Message, model.Exception);
}
break;
case LogType.Error:
{
log.Error(model.Message, model.Exception);
}
break;
case LogType.Warn:
{
log.Warn(model.Message, model.Exception);
}
break;
case LogType.Debug:
{
log.Debug(model.Message, model.Exception);
}
break;
default:
break;
}
model.Dispose();
}
IsProcessing = false;
}
}
}
複製代碼
用到的LoggingModel:
多線程
using System;
namespace Log
{
internal struct LoggingModel : IDisposable
{
private string message;
private LogType messageType;
private Exception exception;
public Exception Exception
{
get { return this.exception; }
set { this.exception = value; }
}
internal LogType MessageType
{
get { return this.messageType; }
set { this.messageType = value; }
}
public string Message
{
get { return this.message; }
set
{
this.message = value;
}
}
public LoggingModel(string message, bool isError = false, Exception ex = null) {
this.message = string.Format("[{0}],{1}", DateTime.UtcNow.ToString("HH:mm:ss,fff"), message);
this.messageType = isError ? LogType.Error : LogType.Info;
this.exception = ex;
}
public LoggingModel(string message, LogType type = LogType.Info, Exception ex = null) {
this.message = string.Format("[{0}] {1}", DateTime.UtcNow.ToString("HH:mm:ss,fff"), message);
this.messageType = type;
this.exception = ex;
}
public void Dispose() {
this.exception = null;
this.message = null;
}
}
internal enum LogType
{
Debug = 0,
Info = 1,
Warn = 2,
Error = 3,
Fatal = 4,
}
}複製代碼
其次,在須要使用logging的工程中加入單獨的配置文件:
app
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<appender name="CommonLogAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="logs\common_"/>
<encoding value="utf-8"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyyMMdd'.log'"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="50MB"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level- %message%newline"/>
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
</root>
<logger name="common">
<level value="INFO" />
<appender-ref ref="CommonLogAppender" />
</logger>
</log4net>
</configuration>複製代碼
而且在app.config或者web.config中指定對應的引用:
異步
<appSettings>
<add key="log4net.Config" value="log4net.config" />
<add key="log4net.Config.Watch" value="True" />
</appSettings>複製代碼
注意:工具
爲防止多線程寫入文件,從而致使出日誌文件名疊加的現象,在配置文件中添加了this
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />複製代碼
出處:spa