NLog在C#中获取消息
NLog get messages in C#
我最近在一个 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
如何获取字符串 "My info"、"My debug"、"My error" 和 "My warn" 作为数组或列表<>?
您可以使用 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
属性呈现
如果您需要将它们写入另一个组件,我建议创建一个 custom target
我最近在一个 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
如何获取字符串 "My info"、"My debug"、"My error" 和 "My warn" 作为数组或列表<>?
您可以使用 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
属性呈现
如果您需要将它们写入另一个组件,我建议创建一个 custom target