我目前在netcoreapp1.1上使用Common.Logging版本3.4.0-beta2和NLog 5.0.0-beta07。 (它和ASP Net Core mvc项目) 我无法找到该案例的Common.Logging.NLog特定版本。
如果它还不可用,我有哪个日志文件选项作为net core的适配器?
答案 0 :(得分:1)
目前没有NLog 5和Common日志包。
替代方案是:
liblog:
或使用Microsoft的日志记录抽象。见docs how to log 并安装NLog.Web.AspNetCore,然后选中install instructions
答案 1 :(得分:1)
您可以在此处(Common.Logging.NLog45)找到自定义的nuget-package:
https://github.com/net-commons/common-logging/pull/176
支持NetCore和结构化日志记录。
答案 2 :(得分:0)
我终于创建了两个类:
public class CommonLoggingNLogAdapter : ILoggerFactoryAdapter
{
public ILog GetLogger(Type type)
{
return new NLogAdapter(type);
}
public ILog GetLogger(string key)
{
return new NLogAdapter(key);
}
}
和
public class NLogAdapter : ILog
{
private Logger log;
public bool IsTraceEnabled { get { return log.IsTraceEnabled; } }
public bool IsDebugEnabled { get { return log.IsDebugEnabled; } }
public bool IsErrorEnabled { get { return log.IsErrorEnabled; } }
public bool IsFatalEnabled { get { return log.IsFatalEnabled; } }
public bool IsInfoEnabled { get { return log.IsInfoEnabled; } }
public bool IsWarnEnabled { get { return log.IsWarnEnabled; } }
public IVariablesContext GlobalVariablesContext => throw new NotImplementedException();
public IVariablesContext ThreadVariablesContext => throw new NotImplementedException();
public INestedVariablesContext NestedThreadVariablesContext => throw new NotImplementedException();
public NLogAdapter(Type type)
{
log = NLog.LogManager.GetLogger(type.ToString());
}
public NLogAdapter(string key)
{
log = NLog.LogManager.GetLogger(key);
}
public void Debug(object message)
{
log.Debug(message);
}
public void Debug(object message, Exception exception)
{
log.Debug(exception, message.ToString());
}
public void Debug(Action<FormatMessageHandler> formatMessageCallback)
{
throw new NotImplementedException();
}
public void Debug(Action<FormatMessageHandler> formatMessageCallback, Exception exception)
{
throw new NotImplementedException();
}
public void Debug(IFormatProvider formatProvider, Action<FormatMessageHandler> formatMessageCallback)
{
throw new NotImplementedException();
}
public void Debug(IFormatProvider formatProvider, Action<FormatMessageHandler> formatMessageCallback, Exception exception)
{
throw new NotImplementedException();
}
public void DebugFormat(string format, params object[] args)
{
throw new NotImplementedException();
}
public void DebugFormat(string format, Exception exception, params object[] args)
{
throw new NotImplementedException();
}
public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args)
{
throw new NotImplementedException();
}
public void DebugFormat(IFormatProvider formatProvider, string format, Exception exception, params object[] args)
{
throw new NotImplementedException();
}
public void Error(object message)
{
log.Error(message);
}
public void Error(object message, Exception exception)
{
log.Error(exception, message.ToString());
}
public void Error(Action<FormatMessageHandler> formatMessageCallback)
{
throw new NotImplementedException();
}
public void Error(Action<FormatMessageHandler> formatMessageCallback, Exception exception)
{
throw new NotImplementedException();
}
public void Error(IFormatProvider formatProvider, Action<FormatMessageHandler> formatMessageCallback)
{
throw new NotImplementedException();
}
public void Error(IFormatProvider formatProvider, Action<FormatMessageHandler> formatMessageCallback, Exception exception)
{
throw new NotImplementedException();
}
public void ErrorFormat(string format, params object[] args)
{
throw new NotImplementedException();
}
public void ErrorFormat(string format, Exception exception, params object[] args)
{
throw new NotImplementedException();
}
public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args)
{
throw new NotImplementedException();
}
public void ErrorFormat(IFormatProvider formatProvider, string format, Exception exception, params object[] args)
{
throw new NotImplementedException();
}
public void Fatal(object message)
{
log.Fatal(message);
}
public void Fatal(object message, Exception exception)
{
log.Fatal(exception, message.ToString());
}
public void Fatal(Action<FormatMessageHandler> formatMessageCallback)
{
throw new NotImplementedException();
}
public void Fatal(Action<FormatMessageHandler> formatMessageCallback, Exception exception)
{
throw new NotImplementedException();
}
public void Fatal(IFormatProvider formatProvider, Action<FormatMessageHandler> formatMessageCallback)
{
throw new NotImplementedException();
}
public void Fatal(IFormatProvider formatProvider, Action<FormatMessageHandler> formatMessageCallback, Exception exception)
{
throw new NotImplementedException();
}
public void FatalFormat(string format, params object[] args)
{
throw new NotImplementedException();
}
public void FatalFormat(string format, Exception exception, params object[] args)
{
throw new NotImplementedException();
}
public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args)
{
throw new NotImplementedException();
}
public void FatalFormat(IFormatProvider formatProvider, string format, Exception exception, params object[] args)
{
throw new NotImplementedException();
}
public void Info(object message)
{
log.Info(message);
}
public void Info(object message, Exception exception)
{
log.Info(exception, message.ToString());
}
public void Info(Action<FormatMessageHandler> formatMessageCallback)
{
throw new NotImplementedException();
}
public void Info(Action<FormatMessageHandler> formatMessageCallback, Exception exception)
{
throw new NotImplementedException();
}
public void Info(IFormatProvider formatProvider, Action<FormatMessageHandler> formatMessageCallback)
{
throw new NotImplementedException();
}
public void Info(IFormatProvider formatProvider, Action<FormatMessageHandler> formatMessageCallback, Exception exception)
{
throw new NotImplementedException();
}
public void InfoFormat(string format, params object[] args)
{
throw new NotImplementedException();
}
public void InfoFormat(string format, Exception exception, params object[] args)
{
throw new NotImplementedException();
}
public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args)
{
throw new NotImplementedException();
}
public void InfoFormat(IFormatProvider formatProvider, string format, Exception exception, params object[] args)
{
throw new NotImplementedException();
}
public void Trace(object message)
{
log.Trace(message);
}
public void Trace(object message, Exception exception)
{
log.Trace(exception, message.ToString());
}
public void Trace(Action<FormatMessageHandler> formatMessageCallback)
{
throw new NotImplementedException();
}
public void Trace(Action<FormatMessageHandler> formatMessageCallback, Exception exception)
{
throw new NotImplementedException();
}
public void Trace(IFormatProvider formatProvider, Action<FormatMessageHandler> formatMessageCallback)
{
throw new NotImplementedException();
}
public void Trace(IFormatProvider formatProvider, Action<FormatMessageHandler> formatMessageCallback, Exception exception)
{
throw new NotImplementedException();
}
public void TraceFormat(string format, params object[] args)
{
throw new NotImplementedException();
}
public void TraceFormat(string format, Exception exception, params object[] args)
{
throw new NotImplementedException();
}
public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args)
{
throw new NotImplementedException();
}
public void TraceFormat(IFormatProvider formatProvider, string format, Exception exception, params object[] args)
{
throw new NotImplementedException();
}
public void Warn(object message)
{
log.Warn(message);
}
public void Warn(object message, Exception exception)
{
log.Warn(exception, message.ToString());
}
public void Warn(Action<FormatMessageHandler> formatMessageCallback)
{
throw new NotImplementedException();
}
public void Warn(Action<FormatMessageHandler> formatMessageCallback, Exception exception)
{
throw new NotImplementedException();
}
public void Warn(IFormatProvider formatProvider, Action<FormatMessageHandler> formatMessageCallback)
{
throw new NotImplementedException();
}
public void Warn(IFormatProvider formatProvider, Action<FormatMessageHandler> formatMessageCallback, Exception exception)
{
throw new NotImplementedException();
}
public void WarnFormat(string format, params object[] args)
{
throw new NotImplementedException();
}
public void WarnFormat(string format, Exception exception, params object[] args)
{
throw new NotImplementedException();
}
public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args)
{
throw new NotImplementedException();
}
public void WarnFormat(IFormatProvider formatProvider, string format, Exception exception, params object[] args)
{
throw new NotImplementedException();
}
}
然后我可以通过执行以下操作将NLog配置为记录器:
Common.Logging.LogManager.Adapter = new CommonLoggingNLogAdapter();
这是我正在实施的临时解决方案,直到正式发布。