从Nlog配置读取? [英] Read from Nlog config?

查看:66
本文介绍了从Nlog配置读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在配置文件中具有以下NLOG目标:

I have the following NLOG target in the config file :

<target xsi:type="File" name="communicationTarget" fileName="${basedir}/logs/${shortdate}.log">
      <layout xsi:type="JsonLayout" includeAllProperties="true" maxRecursionLimit="10">
        <attribute name="message" layout="${message}" />
      </layout>
    </target>

有什么方法可以从C#代码中读取maxRecursionLimit吗?

Is there any way to read the maxRecursionLimit from C# code?

我已经尝试过这些:

LogManager.Configuration.AllTargets.Where(c => c.Name == "communicationTarget").FirstOrDefault();
LogManager.Configuration.FindTargetByName<FileTarget>("communicationTarget");

到目前为止未找到maxRecursionLimit.

No maxRecursionLimit found so far.

推荐答案

也许是这样的:

var myTarget = LogManager.Configuration.FindTargetByName("communicationTarget");
var myTarget = ((myTarget as NLog.Targets.Wrappers.WrapperTargetBase)?.WrappedTarget) ?? myTarget;
var jsonLayout = (myTarget as NLog.Targets.TargetWithLayout)?.Layout as NLog.Layouts.JsonLayout;
var maxRecursionLimit = jsonLayout?.MaxRecursionLimit;

这还将处理您应激活< targets async ="true"> .

P.S.尚未尝试编译代码:)

P.S. Have not tried to compile the code :)

这篇关于从Nlog配置读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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