我使用的是我发现的here的NLogTraceListener
。
它在我的配置文件中定义如下:
<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)
答案 0 :(得分:1)
traceOutputOptions
中不支持NLogTraceListener
。
您只需将布局配置为包括${callsite}
。
示例:
layout="${longdate} ${logger} ${uppercase:${level}} ${threadid} ${message} ${exception:format=tostring} ${callsite}"
有关如何配置输出选项的信息,请参见Wiki:https://github.com/NLog/NLog/wiki/Callsite-layout-renderer
答案 1 :(得分:0)
更新:
一开始误读了你的问题。
如果可能,应该记录堆栈跟踪信息,请参见
如果这样做没有帮助,则可以使用自定义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>