简体   繁体   English

NLog在C#中获取消息

[英]NLog get messages in C#

I am recently using Nlog in an ASP.Net project for a client with good success. 我最近在ASP.Net项目中为客户取得了成功,使用了Nlog。 I need to connect to an existing application that has its own event log management and provide them with a copy of my log information. 我需要连接到具有自己的事件日志管理的现有应用程序,并向他们提供我的日志信息的副本。

Is there any way to get the strings that I provided to Nlog using any of the log functions? 有没有办法使用任何日志功能来获取我提供给Nlog的字符串?

Example: 例:

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

How I can get strings "My info", "My debug", "My error" and "My warn" as an array or List<>? 如何获取字符串“我的信息”,“我的调试”,“我的错误”和“我的警告”作为数组或List <>?

You could use the MemoryTarget for it: 您可以使用MemoryTarget

nlog.config: 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#: C#:

var target = LogManager.Configuration.FindTargetByName<MemoryTarget>("target1");
IList<string> messages = target.Logs;

The messages are strings , rendered from the layout attribute in nlog.config 消息是strings ,从nlog.config中的layout属性呈现

API docs for MemoryTarget 适用于MemoryTarget的API文档

If you need to write them to another component, I would recommend to create a custom target 如果您需要将它们写入其他组件,则建议创建一个自定义目标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM