如何獲得來自自定義佈局渲染器的整數值?NLog自定義佈局渲染器整數
我在嘗試使用自定義佈局渲染器設置需要整數的目標的屬性時收到異常。通過單步執行代碼,它看起來像是在寫入目標的日誌條目處將自定義佈局渲染器替換爲它們的值。拋出的異常在創建logger對象時發生 - 大概是在處理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配置,請告訴我們。
任何幫助將不勝感激。