NLogTraceListener 如何输出traceOutputOptions
NLogTraceListener how to output traceOutputOptions
我正在使用我找到的 NLogTraceListener
here。
在我的配置文件中定义如下:
<sharedListeners>
<add name="nlog" type="NLog.NLogTraceListener, NLog" traceOutputOptions="Callstack" />
</sharedListeners>
这里是 nlog.config 文件中的 LayoutRenderer:
layout="${longdate} ${logger} ${uppercase:${level}} ${threadid} ${message} ${exception:format=tostring}"
这里是错误输出到日志文件的示例:
2018-08-07 06:37:46.3350 System.Net.Sockets ERROR 200 [16956] Exception in Socket#8977203::Connect - An operation was attempted on something that is not a socket 23.96.28.38:443.
如何让我的布局包含 traceOutputOptions
(在本例中为 callStack)
更新:
一开始看错了你的问题。
如果可能,应记录堆栈跟踪,请参阅
如果这没有帮助,那么您可以使用自定义 TraceListener 来拯救您。
- 继承自
NLogTraceListener
。
- 覆盖
ProcessLogEventInfo
。另见 original code of NLogTraceListener
例如(C#)
public class MyNLogTraceListener : NLogTraceListener
{
protected override void ProcessLogEventInfo(LogLevel logLevel, string loggerName, [Localizable(false)] string message, object[] arguments, int? eventId, TraceEventType? eventType, Guid? relatedActiviyId)
{
//copy of NLog
loggerName = (loggerName ?? Name) ?? string.Empty;
StackTrace stackTrace = null;
int userFrameIndex = -1;
if (AutoLoggerName)
{
stackTrace = new StackTrace();
for (int i = 0; i < stackTrace.FrameCount; ++i)
{
var frame = stackTrace.GetFrame(i);
loggerName = Internal.StackTraceUsageUtils.LookupClassNameFromStackFrame(frame);
if (!string.IsNullOrEmpty(loggerName))
{
userFrameIndex = i;
break;
}
}
}
ILogger logger;
if (LogFactory != null)
{
logger = LogFactory.GetLogger(loggerName);
}
else
{
logger = LogManager.GetLogger(loggerName);
}
logLevel = ForceLogLevel ?? logLevel;
if (!logger.IsEnabled(logLevel))
{
return; // We are done
}
var ev = new LogEventInfo();
ev.LoggerName = loggerName;
ev.Level = logLevel;
if (eventType.HasValue)
{
ev.Properties.Add("EventType", eventType.Value);
}
if (relatedActiviyId.HasValue)
{
ev.Properties.Add("RelatedActivityID", relatedActiviyId.Value);
}
ev.Message = message;
ev.Parameters = arguments;
ev.Level = ForceLogLevel ?? logLevel;
if (eventId.HasValue)
{
ev.Properties.Add("EventID", eventId.Value);
}
if (stackTrace != null && userFrameIndex >= 0)
{
ev.SetStackTrace(stackTrace, userFrameIndex);
}
//something here with base.TraceOutputOptions <--------------------
/
logger.Log(ev);
}
}
用法:
<sharedListeners>
<add name="nlog" type="MyNameSpace.MyNLogTraceListener, MyDllName" traceOutputOptions="Callstack" />
</sharedListeners>
NLogTraceListener
中不支持 traceOutputOptions
。
相反,您只需配置布局以包含 ${callsite}
。
示例:
layout="${longdate} ${logger} ${uppercase:${level}} ${threadid} ${message} ${exception:format=tostring} ${callsite}"
有关如何配置输出选项的信息,请参阅 Wiki:https://github.com/NLog/NLog/wiki/Callsite-layout-renderer
我正在使用我找到的 NLogTraceListener
here。
在我的配置文件中定义如下:
<sharedListeners>
<add name="nlog" type="NLog.NLogTraceListener, NLog" traceOutputOptions="Callstack" />
</sharedListeners>
这里是 nlog.config 文件中的 LayoutRenderer:
layout="${longdate} ${logger} ${uppercase:${level}} ${threadid} ${message} ${exception:format=tostring}"
这里是错误输出到日志文件的示例:
2018-08-07 06:37:46.3350 System.Net.Sockets ERROR 200 [16956] Exception in Socket#8977203::Connect - An operation was attempted on something that is not a socket 23.96.28.38:443.
如何让我的布局包含 traceOutputOptions
(在本例中为 callStack)
更新:
一开始看错了你的问题。
如果可能,应记录堆栈跟踪,请参阅
如果这没有帮助,那么您可以使用自定义 TraceListener 来拯救您。
- 继承自
NLogTraceListener
。 - 覆盖
ProcessLogEventInfo
。另见 original code of NLogTraceListener
例如(C#)
public class MyNLogTraceListener : NLogTraceListener
{
protected override void ProcessLogEventInfo(LogLevel logLevel, string loggerName, [Localizable(false)] string message, object[] arguments, int? eventId, TraceEventType? eventType, Guid? relatedActiviyId)
{
//copy of NLog
loggerName = (loggerName ?? Name) ?? string.Empty;
StackTrace stackTrace = null;
int userFrameIndex = -1;
if (AutoLoggerName)
{
stackTrace = new StackTrace();
for (int i = 0; i < stackTrace.FrameCount; ++i)
{
var frame = stackTrace.GetFrame(i);
loggerName = Internal.StackTraceUsageUtils.LookupClassNameFromStackFrame(frame);
if (!string.IsNullOrEmpty(loggerName))
{
userFrameIndex = i;
break;
}
}
}
ILogger logger;
if (LogFactory != null)
{
logger = LogFactory.GetLogger(loggerName);
}
else
{
logger = LogManager.GetLogger(loggerName);
}
logLevel = ForceLogLevel ?? logLevel;
if (!logger.IsEnabled(logLevel))
{
return; // We are done
}
var ev = new LogEventInfo();
ev.LoggerName = loggerName;
ev.Level = logLevel;
if (eventType.HasValue)
{
ev.Properties.Add("EventType", eventType.Value);
}
if (relatedActiviyId.HasValue)
{
ev.Properties.Add("RelatedActivityID", relatedActiviyId.Value);
}
ev.Message = message;
ev.Parameters = arguments;
ev.Level = ForceLogLevel ?? logLevel;
if (eventId.HasValue)
{
ev.Properties.Add("EventID", eventId.Value);
}
if (stackTrace != null && userFrameIndex >= 0)
{
ev.SetStackTrace(stackTrace, userFrameIndex);
}
//something here with base.TraceOutputOptions <--------------------
/
logger.Log(ev);
}
}
用法:
<sharedListeners>
<add name="nlog" type="MyNameSpace.MyNLogTraceListener, MyDllName" traceOutputOptions="Callstack" />
</sharedListeners>
NLogTraceListener
中不支持 traceOutputOptions
。
相反,您只需配置布局以包含 ${callsite}
。
示例:
layout="${longdate} ${logger} ${uppercase:${level}} ${threadid} ${message} ${exception:format=tostring} ${callsite}"
有关如何配置输出选项的信息,请参阅 Wiki:https://github.com/NLog/NLog/wiki/Callsite-layout-renderer