本文介绍了为什么UserStackFrame为null并且我必须设置它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用NLOG记录信息。有一个属性 UserStackFrame 。 我使用了代码。I am using NLOG to log the information. There is a property UserStackFrame.I used the code.protected override void Write(LogEventInfo loggingEvent) { try { var renderedEvent = Layout.Render(loggingEvent); var messageBuilder = new GelfMessageBuilder(renderedEvent, HostName, loggingEvent.TimeStamp, ToGelf(loggingEvent.Level)) .SetAdditionalField(GelfAdditionalFields.Facility, Facility) .SetAdditionalField(GelfAdditionalFields.LoggerName, loggingEvent.LoggerName); if (IncludeSource) { var userStackFrame = loggingEvent.UserStackFrame; if (userStackFrame != null) { messageBuilder.SetAdditionalField(GelfAdditionalFields.SourceFileName, userStackFrame.GetFileName()); messageBuilder.SetAdditionalField(GelfAdditionalFields.SourceLineNumber, userStackFrame.GetFileLineNumber().ToString(CultureInfo.InvariantCulture)); } } 但 userStackFrame 为空。我不知道为什么我应该设置它? 如果是这样,怎么样? 谢谢。But userStackFrame is null. I don't know why and should I need to set up it?If so, how?Thanks.推荐答案protected override void Write(LogEventInfo loggingEvent) { try { var renderedEvent = Layout.Render(loggingEvent); var stack = new StackTrace(); int userFrameIndex = stack.FrameCount; loggingEvent.setStackTrace(stack,userFrameIndex); var messageBuilder = new GelfMessageBuilder(renderedEvent, HostName, loggingEvent.TimeStamp, ToGelf(loggingEvent.Level)) .SetAdditionalField(GelfAdditionalFields.Facility, Facility) .SetAdditionalField(GelfAdditionalFields.LoggerName, loggingEvent.LoggerName); if (IncludeSource) { if(loggingEvent.HasStackTrace) { var userStackFrame = loggingEvent.UserStackFrame; } if (userStackFrame != null) { messageBuilder.SetAdditionalField(GelfAdditionalFields.SourceFileName, userStackFrame.GetFileName()); messageBuilder.SetAdditionalField(GelfAdditionalFields.SourceLineNumber, userStackFrame.GetFileLineNumber().ToString(CultureInfo.InvariantCulture)); } } 我希望这可以帮到你。 最好的问候!I hope this can help you.Best Regards! 这篇关于为什么UserStackFrame为null并且我必须设置它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-11 01:35