LCL日志提供了一个公共接口,如果需要的话,可以很容易的更改它
Logger.LogInfo("就一行是不是很简单 ");
实现自己的日志需要继承LoggerBase类 如下示例:
public class FileLogger : LoggerBase
{
public override void LogInfo(string message)
{
string msg = string.Format(@"{0}|{1}|Inform|{2}" + Environment.NewLine, DateTime.Now,
Thread.CurrentThread.ManagedThreadId, message);
AppendAllText(msg);
}
public override void LogWarn(string message)
{
string msg = string.Format(@"{0}|{1}|Warn|{2}" + Environment.NewLine, DateTime.Now,
Thread.CurrentThread.ManagedThreadId, message);
AppendAllText(msg, LogType.Warn);
}
public override void LogDebug(string message)
{
string msg = string.Format(@"{0}|{1}|Debug|{2}" + Environment.NewLine, DateTime.Now,
Thread.CurrentThread.ManagedThreadId, message);
AppendAllText(msg, LogType.Debug);
}
public override void LogError(string title, Exception e)
{
var stackTrace = e.StackTrace;
e = e.GetBaseException();
string msg = string.Format(@"{0}|{1}|Error|{2}" + Environment.NewLine, DateTime.Now,
Thread.CurrentThread.ManagedThreadId, title + e.ToString());
AppendAllText(msg, LogType.Error);
}
public override void LogDbAccessed(string sql, IDbDataParameter[] parameters,
DbConnectionSchema connectionSchema)
{
var content = sql;
if (parameters.Length > 0)
{
var pValues = parameters.Select(p =>
{
var value = p.Value;
if (value is string)
{
value = '"' + value.ToString() + '"';
}
return value;
});
content += Environment.NewLine + "Parameters:" + string.Join(",", pValues);
}
content = DateTime.Now + Environment.NewLine + "Database: " + connectionSchema.Database +
Environment.NewLine + content + "\r\n\r\n\r\n";
AppendAllText(content, LogType.Trace);
}
private void AppendAllText(string contents, LogType logType = LogType.Info)
{
string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "/"
+ EngineContext.Current.Resolve《lconfig》().LoggerPath;
string logfile = path + "/" + logType + DateTime.Now.ToString("yyyyMMdd") + "log.log";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
File.AppendAllText(logfile, contents);
}
}
在项目启动的地方注入自定义日志就可以了,如在Global.asax文件中写入如下代码
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { Logger.SetImplementation(new FileLogger()); } }