Common.Logging.NLog 网络核心不可用?
Common.Logging.NLog not available for net core?
我目前在 netcoreapp1.1 上使用 Common.Logging 版本 3.4.0-beta2 和 NLog 5.0.0-beta07。 (它和 ASP Net Core mvc 项目)
我找不到 Common.Logging.NLog 特定版本。
如果它还不可用,我有哪个日志文件选项作为 net core 的适配器?
目前没有用于 NLog 5 和通用日志记录的包。
备选方案是:
库日志:
- 直接复制the file liblog.cs, see website
- 手动将命名空间 YourRootNamespace 重命名为项目的根命名空间。
- 启用 LIBLOG_PORTABLE 编译器指令。
或者使用微软的日志抽象。参见 docs how to log
并安装 NLog.Web.AspNetCore, and check install instructions
我终于创建了两个类:
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();
这是我在正式发布之前实施的临时解决方案。
您可以在此处找到自定义构建 nuget-package (Common.Logging.NLog45):
https://github.com/net-commons/common-logging/pull/176
支持 NetCore 和结构化日志记录。
我目前在 netcoreapp1.1 上使用 Common.Logging 版本 3.4.0-beta2 和 NLog 5.0.0-beta07。 (它和 ASP Net Core mvc 项目) 我找不到 Common.Logging.NLog 特定版本。
如果它还不可用,我有哪个日志文件选项作为 net core 的适配器?
目前没有用于 NLog 5 和通用日志记录的包。
备选方案是:
库日志:
- 直接复制the file liblog.cs, see website
- 手动将命名空间 YourRootNamespace 重命名为项目的根命名空间。
- 启用 LIBLOG_PORTABLE 编译器指令。
或者使用微软的日志抽象。参见 docs how to log 并安装 NLog.Web.AspNetCore, and check install instructions
我终于创建了两个类:
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();
这是我在正式发布之前实施的临时解决方案。
您可以在此处找到自定义构建 nuget-package (Common.Logging.NLog45):
https://github.com/net-commons/common-logging/pull/176
支持 NetCore 和结构化日志记录。