NLog在C#中获取消息 [英] NLog get messages in C#

查看:272
本文介绍了NLog在C#中获取消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在ASP.Net项目中将Nlog用于具有良好成功的客户端.我需要连接到具有自己的事件日志管理功能的现有应用程序,并向其提供我的日志信息的副本.

I am recently using Nlog in an ASP.Net project for a client with good success. 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.

是否可以使用任何日志功能获取提供给Nlog的字符串?

Is there any way to get the strings that I provided to Nlog using any of the log functions?

示例:

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<>?

推荐答案

您可以使用 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#:

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

消息是strings,由nlog.config中的layout属性呈现

The messages are strings, rendered from the layout attribute in nlog.config

MemoryTarget的API文档

如果您需要将它们写入另一个组件,我建议创建一个自定义目标

If you need to write them to another component, I would recommend to create a custom target

这篇关于NLog在C#中获取消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆