NLog自定义布局渲染器整数

时间:2016-05-23 13:56:21

标签: layout integer nlog renderer

如何从自定义布局渲染器获取整数值?

尝试使用自定义布局渲染器设置需要整数的目标属性时,我收到异常。通过单步执行代码,看起来自定义布局渲染器将被替换为在写入目标的日志条目时的值。在创建记录器对象时发生异常 - 并且可能在处理NLog.config时以及当它看到对我的自定义布局渲染器而不是整数的引用时,抛出异常。

这是自定义布局渲染器:

[LayoutRenderer("buffer-size")]
public class BufferSizeLayoutRenderer : LayoutRenderer
{
    protected override void Append(StringBuilder builder, LogEventInfo logEvent)
    {
        int BufferSize=150;  // would actually read from externally managed settings
        builder.Append(BufferSize);
    }
}

我在NLog.config中注册了扩展程序集。

自定义布局渲染器的引用位于BufferingWrapper目标中,如下所示:

<target xsi:type="BufferingWrapper" 
    name="InfoBufferingTarget"
    bufferSize="${buffer-size}"
    flushTimeout="60000"
    slidingTimeout="true">

当执行创建记录器对象的代码行时,抛出此异常:

NLog.NLogConfigurationException was unhandled by user code
    HResult=-2146233088
Message=Error when setting property 'BufferSize' on BufferingWrapper Target[InfoBufferingTarget]()
Source=NLog
StackTrace:
    at NLog.Internal.PropertyHelper.SetPropertyFromString(Object o, String name, String value, ConfigurationItemFactory configurationItemFactory)
    at NLog.Config.XmlLoggingConfiguration.ConfigureObjectFromAttributes(Object targetObject, NLogXmlElement element, Boolean ignoreType)
    at NLog.Config.XmlLoggingConfiguration.ParseTargetElement(Target target, NLogXmlElement targetElement)
    at NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement)
    at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String baseDirectory)
    at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String baseDirectory)
    at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
    at NLog.Config.XmlLoggingConfiguration..ctor(String fileName)
    at NLog.LogFactory.LoadLoggingConfiguration(String configFile)
    at NLog.LogFactory.get_Configuration()
    at NLog.LogFactory.GetLogger(LoggerCacheKey cacheKey)
    at NLog.LogFactory.GetLogger(String name)
    at NLog.LogManager.GetCurrentClassLogger()
    at MyCompany.NLogRepository..ctor() in c:\Source\NLogRepository.cs:line 19
    at MyCompany.Controllers.LogController.Post(Data data) in c:\Source\LogController.cs:line 21
    at lambda_method(Closure , Object , Object[] )
    at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClassc.<GetExecutor>b__6(Object instance, Object[] methodParameters)
    at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
    at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
InnerException: 
    HResult=-2146233088
    Message=${buffer-size} is not a valid value for Int32.
    Source=System
    StackTrace:
         at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
         at System.ComponentModel.TypeConverter.ConvertFromInvariantString(String text)
         at NLog.Internal.PropertyHelper.TryTypeConverterConversion(Type type, String value, Object& newValue)
         at NLog.Internal.PropertyHelper.SetPropertyFromString(Object o, String name, String value, ConfigurationItemFactory configurationItemFactory)
    InnerException: System.FormatException
         HResult=-2146233033
         Message=Input string was not in a correct format.
         Source=mscorlib
         StackTrace:
               at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
               at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
               at System.ComponentModel.Int32Converter.FromString(String value, NumberFormatInfo formatInfo)
               at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
         InnerException: 

在执行自定义布局渲染器中的代码之前抛出此异常。

我的最终目标 - 我需要能够在运行时修改NLog设置,而不会覆盖NLog.config,在我的情况下需要完整的应用程序部署。我希望能够调整BufferingWrapper设置并查看对性能的影响。如果有另一种方法可以在不使用自定义布局渲染器的情况下完成此操作,并且没有在代码中完全创建NLog配置,请告诉我。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

该属性必须是Layout,因此需要string

你需要一个渲染,然后解析它。 E.g。

Layout l = "${..}"
var size = Int.Parse(l.Render(logEventInfo)) //todo better checking