我最近在ASP.Net项目中为客户取得了成功,使用了Nlog。我需要连接到具有自己的事件日志管理的现有应用程序,并向他们提供我的日志信息的副本。是否有任何方法可以使用任何日志功能获取我提供给Nlog的字符串?例:logger.Info("My info");logger.Debug("My debug");logger.Error("My error");logger.Warn("My warn");List<string> messages = logger.getStrings() <-- this is what I need如何获取字符串“我的信息”,“我的调试”,“我的错误”和“我的警告”作为数组或List ? 最佳答案 您可以使用MemoryTarget:nlog.config:<nlog internalLogFile="c:\tempp\nlog-internal.log" internalLogLevel="Info"> <targets> <target xsi:type="Memory" name="target1" layout="${message}" /> </targets> <rules> <logger name="*" writeTo="target1" /> </rules></nlog>C#:var target = LogManager.Configuration.FindTargetByName<MemoryTarget>("target1");IList<string> messages = target.Logs;消息是strings,从nlog.config中的layout属性呈现API docs for MemoryTarget如果您需要将它们写入另一个组件,我建议创建一个custom target关于c# - NLog在C#中获取消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44933339/
10-13 06:21