Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NLog自定义文件目标_C#_Nlog_Sentinel - Fatal编程技术网

C# NLog自定义文件目标

C# NLog自定义文件目标,c#,nlog,sentinel,C#,Nlog,Sentinel,我为哨兵定制了一个NLogViewerTarget(NLogViewerEx),如下所示: 现在,我还想为我的FileTarget设置格式化消息,但当我应用它时,它不再工作(记录) 我给文件目标提供了xsi:type FileTargetEx和xsi:type NLogViewerEx,但没有成功。如果我这样做了,Sentinel和File将不再记录日志 调试 启用内部调试日志后,我得到以下日志: 2017-12-13 09:32:52.6572 Info Loading assembly: H

我为
哨兵
定制了一个
NLogViewerTarget
NLogViewerEx),如下所示:

现在,我还想为我的
FileTarget
设置格式化消息,但当我应用它时,它不再工作(记录)

我给
文件目标提供了
xsi:type FileTargetEx
xsi:type NLogViewerEx
,但没有成功。如果我这样做了,Sentinel和File将不再记录日志

调试 启用内部调试日志后,我得到以下日志:

2017-12-13 09:32:52.6572 Info Loading assembly: HtCore
2017-12-13 09:32:52.6834 Debug ScanAssembly('NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c')
2017-12-13 09:32:52.7356 Debug Start auto loading, location: D:\Repositories\gitlab.company\dotNet\HtCore\HtCore\TestConsole\bin\Debug
2017-12-13 09:32:52.7356 Debug Auto loading done
2017-12-13 09:32:52.7496 Debug ScanAssembly('HtCore, Version=1.4.0.8, Culture=neutral, PublicKeyToken=null')
2017-12-13 09:32:52.7797 Debug Setting 'UppercaseLayoutRendererWrapper.uppercase' to 'true'
2017-12-13 09:32:52.7797 Debug Setting 'FileTargetEx.name' to 'file'
2017-12-13 09:32:52.7978 Debug Setting 'FileTargetEx.filename' to '${specialfolder:folder=commonapplicationdata}/company gmbh/Testconsole/logs/Testconsole.log'
2017-12-13 09:32:52.7978 Debug Setting 'SpecialFolderLayoutRenderer.folder' to 'commonapplicationdata'
2017-12-13 09:32:52.8108 Debug Setting 'FileTargetEx.keepfileopen' to 'false'
2017-12-13 09:32:52.8108 Debug Setting 'FileTargetEx.archivefilename' to '${specialfolder:folder=commonapplicationdata}/company gmbh/Testconsole/logs/Testconsole_archive.{##}.zip'
2017-12-13 09:32:52.8108 Debug Setting 'SpecialFolderLayoutRenderer.folder' to 'commonapplicationdata'
2017-12-13 09:32:52.8278 Debug Setting 'FileTargetEx.archivenumbering' to 'dateandsequence'
2017-12-13 09:32:52.8278 Debug Setting 'FileTargetEx.archiveevery' to 'day'
2017-12-13 09:32:52.8429 Debug Setting 'FileTargetEx.maxarchivefiles' to '50'
2017-12-13 09:32:52.8429 Debug Setting 'FileTargetEx.archivedateformat' to 'yyyy-mm-dd'
2017-12-13 09:32:52.8429 Debug Setting 'FileTargetEx.archiveoldfileonstartup' to 'true'
2017-12-13 09:32:52.8634 Debug Setting 'FileTargetEx.createdirs' to 'true'
2017-12-13 09:32:52.8634 Debug Setting 'FileTargetEx.enablearchivefilecompression' to 'true'
2017-12-13 09:32:52.8758 Debug Setting 'FileTargetEx.encoding' to 'utf-8'
2017-12-13 09:32:52.8758 Debug Setting 'FileTargetEx.header' to '############################################## Testconsole log ##############################################'
2017-12-13 09:32:52.8968 Warn Error when setting '############################################## ${appname} log ##############################################' on attibute 'header'
2017-12-13 09:32:52.9028 Error Parsing configuration from D:\Repositories\gitlab.company\dotNet\HtCore\HtCore\TestConsole\bin\Debug\NLog.config failed. Exception: NLog.NLogConfigurationException: Exception when parsing D:\Repositories\gitlab.company\dotNet\HtCore\HtCore\TestConsole\bin\Debug\NLog.config.  ---> NLog.NLogConfigurationException: Error when setting property 'Header' on FileTargetEx Target[file] ---> System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
   bei NLog.Targets.TargetWithLayoutHeaderAndFooter.set_Header(Layout value)
   --- Ende der internen Ausnahmestapelüberwachung ---
   bei NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
   bei NLog.Config.XmlLoggingConfiguration.ConfigureObjectFromAttributes(Object targetObject, NLogXmlElement element, Boolean ignoreType)
   bei NLog.Config.XmlLoggingConfiguration.ParseTargetElement(Target target, NLogXmlElement targetElement, Dictionary`2 typeNameToDefaultTargetParameters)
   bei NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement)
   bei NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
   bei NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String filePath, Boolean autoReloadDefault)
   bei NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
   --- Ende der internen Ausnahmestapelüberwachung ---
2017-12-13 09:32:52.9028 Debug --- NLog configuration dump ---
Nlog.conf 我总是犯同样的错误。无论我是将
FileLayout
设置为
Layout
,还是将其他
Layout
设置为
Sentinel
,这都无关紧要

当取消注释
布局覆盖
时,它正在加载NLog.conf..

工作解决方案 如果有更好的解决方案,请告诉我

/// <summary>
/// is using the <see cref="FileLayoutEx"/> class.
/// </summary>
[Target("FileTargetEx")]
internal class FileTargetEx : FileTarget
{
    public override Layout Layout
    {
        get => _Layout;
        set
        {
            base.Layout = new FileLayoutEx((value as SimpleLayout).OriginalText);
            _Layout = new FileLayoutEx((value as SimpleLayout).OriginalText);
        }
    }
    private Layout _Layout;

    public FileTargetEx()
    {
        Footer = new FileLayoutEx();
        Header = new FileLayoutEx();
    }
}

/// <summary>
/// Provides a formated message (Exception included!).
/// </summary>
[Layout("FileLayoutEx")]
internal class FileLayoutEx : SimpleLayout
{
    public FileLayoutEx() { }

    public FileLayoutEx(string originalText) : base(originalText) { }

    protected override string GetFormattedMessage(LogEventInfo logEvent)
    {
        //Extension method is outsourced
        LogEventInfo updatedInfo = logEvent.GetFormattedMessage();
        return base.GetFormattedMessage(updatedInfo);
    }
}
//
///正在使用该类。
/// 
[目标(“FileTargetEx”)]
内部类FileTargetEx:FileTarget
{
公共覆盖布局
{
get=>\u布局;
设置
{
base.Layout=newfilelayoutex((值为SimpleLayout.OriginalText);
_Layout=新文件LayoutEx((值为SimpleLayout).OriginalText);
}
}
私人布局(u布局),;
公共文件targetex()
{
Footer=newfilelayoutex();
Header=newfilelayoutex();
}
}
/// 
///提供格式化的消息(包括异常!)。
/// 
[布局(“FileLayoutEx”)]
内部类FileLayoutEx:SimpleLayout
{
公共文件LayoutEx(){}
public FileLayoutEx(string originalText):基本(originalText){}
受保护的重写字符串GetFormattedMessage(LogEventInfo logEvent)
{
//扩展方法是外包的
LogEventInfo UpdateInfo=logEvent.GetFormattedMessage();
返回base.GetFormattedMessage(updateInfo);
}
}
NLog.conf目标


您正在使用xsi:type=“NLogViewerEx”两次。您需要为自定义FileTarget指定xsi:type=“FileTargetEx”。啊,对不起,我已经试过了!但是现在再也没有(哨兵和文件)日志记录了!您是否尝试启用InternalLogger以查看NLog如何处理您的自定义目标:谢谢您的提示!现在我知道这是因为
布局
,但我仍然无法让它工作。我添加了更多详细信息/日志输出。
2017-12-13 09:32:52.6572 Info Loading assembly: HtCore
2017-12-13 09:32:52.6834 Debug ScanAssembly('NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c')
2017-12-13 09:32:52.7356 Debug Start auto loading, location: D:\Repositories\gitlab.company\dotNet\HtCore\HtCore\TestConsole\bin\Debug
2017-12-13 09:32:52.7356 Debug Auto loading done
2017-12-13 09:32:52.7496 Debug ScanAssembly('HtCore, Version=1.4.0.8, Culture=neutral, PublicKeyToken=null')
2017-12-13 09:32:52.7797 Debug Setting 'UppercaseLayoutRendererWrapper.uppercase' to 'true'
2017-12-13 09:32:52.7797 Debug Setting 'FileTargetEx.name' to 'file'
2017-12-13 09:32:52.7978 Debug Setting 'FileTargetEx.filename' to '${specialfolder:folder=commonapplicationdata}/company gmbh/Testconsole/logs/Testconsole.log'
2017-12-13 09:32:52.7978 Debug Setting 'SpecialFolderLayoutRenderer.folder' to 'commonapplicationdata'
2017-12-13 09:32:52.8108 Debug Setting 'FileTargetEx.keepfileopen' to 'false'
2017-12-13 09:32:52.8108 Debug Setting 'FileTargetEx.archivefilename' to '${specialfolder:folder=commonapplicationdata}/company gmbh/Testconsole/logs/Testconsole_archive.{##}.zip'
2017-12-13 09:32:52.8108 Debug Setting 'SpecialFolderLayoutRenderer.folder' to 'commonapplicationdata'
2017-12-13 09:32:52.8278 Debug Setting 'FileTargetEx.archivenumbering' to 'dateandsequence'
2017-12-13 09:32:52.8278 Debug Setting 'FileTargetEx.archiveevery' to 'day'
2017-12-13 09:32:52.8429 Debug Setting 'FileTargetEx.maxarchivefiles' to '50'
2017-12-13 09:32:52.8429 Debug Setting 'FileTargetEx.archivedateformat' to 'yyyy-mm-dd'
2017-12-13 09:32:52.8429 Debug Setting 'FileTargetEx.archiveoldfileonstartup' to 'true'
2017-12-13 09:32:52.8634 Debug Setting 'FileTargetEx.createdirs' to 'true'
2017-12-13 09:32:52.8634 Debug Setting 'FileTargetEx.enablearchivefilecompression' to 'true'
2017-12-13 09:32:52.8758 Debug Setting 'FileTargetEx.encoding' to 'utf-8'
2017-12-13 09:32:52.8758 Debug Setting 'FileTargetEx.header' to '############################################## Testconsole log ##############################################'
2017-12-13 09:32:52.8968 Warn Error when setting '############################################## ${appname} log ##############################################' on attibute 'header'
2017-12-13 09:32:52.9028 Error Parsing configuration from D:\Repositories\gitlab.company\dotNet\HtCore\HtCore\TestConsole\bin\Debug\NLog.config failed. Exception: NLog.NLogConfigurationException: Exception when parsing D:\Repositories\gitlab.company\dotNet\HtCore\HtCore\TestConsole\bin\Debug\NLog.config.  ---> NLog.NLogConfigurationException: Error when setting property 'Header' on FileTargetEx Target[file] ---> System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
   bei NLog.Targets.TargetWithLayoutHeaderAndFooter.set_Header(Layout value)
   --- Ende der internen Ausnahmestapelüberwachung ---
   bei NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
   bei NLog.Config.XmlLoggingConfiguration.ConfigureObjectFromAttributes(Object targetObject, NLogXmlElement element, Boolean ignoreType)
   bei NLog.Config.XmlLoggingConfiguration.ParseTargetElement(Target target, NLogXmlElement targetElement, Dictionary`2 typeNameToDefaultTargetParameters)
   bei NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement)
   bei NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
   bei NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String filePath, Boolean autoReloadDefault)
   bei NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
   --- Ende der internen Ausnahmestapelüberwachung ---
2017-12-13 09:32:52.9028 Debug --- NLog configuration dump ---
<?xml version="1.0" encoding="utf-8" ?>
<nlog
  xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
  autoReload="true"
  internalLogFile="c:\log.txt" 
  internalLogLevel="Debug">
  <extensions>
    <add assembly="HtCore"/>
  </extensions>
  <variable name="appname" value="Testconsole" />

  <targets async="true">
    <target
      xsi:type="FileTargetEx"
      name="file"
      filename="${specialfolder:folder=commonapplicationdata}/company gmbh/${appname}/logs/${appname}.log"
      keepfileopen="false"
      archivefilename="${specialfolder:folder=commonapplicationdata}/company gmbh/${appname}/logs/${appname}_archive.{##}.zip"
      archivenumbering="dateandsequence"
      archiveevery="day"
      maxarchivefiles="50"
      archivedateformat="yyyy-mm-dd"
      archiveoldfileonstartup="true"
      createdirs="true"
      enablearchivefilecompression="true"
      encoding="utf-8"
      header="############################################## ${appname} log ##############################################"
      archiveabovesize="10485760"/>


    <target xsi:type="NLogViewerEx"
              name="sentinel"
              address="udp://127.0.0.1:9999"/>
  </targets>
  <rules>
    <logger name="*" writeTo="file" minlevel="Info"/>
    <logger name="*" writeTo="sentinel" minlevel="Info"/>
  </rules>
</nlog>
[Target("FileTargetEx")]
internal class FileTargetEx : NLog.Targets.FileTarget
{
    private readonly FileLayout _Layout = new FileLayout();

    public override Layout Layout { get { return _Layout; } set { } }
}

internal class FileLayout : NLog.Layouts.SimpleLayout
{

}
/// <summary>
/// is using the <see cref="FileLayoutEx"/> class.
/// </summary>
[Target("FileTargetEx")]
internal class FileTargetEx : FileTarget
{
    public override Layout Layout
    {
        get => _Layout;
        set
        {
            base.Layout = new FileLayoutEx((value as SimpleLayout).OriginalText);
            _Layout = new FileLayoutEx((value as SimpleLayout).OriginalText);
        }
    }
    private Layout _Layout;

    public FileTargetEx()
    {
        Footer = new FileLayoutEx();
        Header = new FileLayoutEx();
    }
}

/// <summary>
/// Provides a formated message (Exception included!).
/// </summary>
[Layout("FileLayoutEx")]
internal class FileLayoutEx : SimpleLayout
{
    public FileLayoutEx() { }

    public FileLayoutEx(string originalText) : base(originalText) { }

    protected override string GetFormattedMessage(LogEventInfo logEvent)
    {
        //Extension method is outsourced
        LogEventInfo updatedInfo = logEvent.GetFormattedMessage();
        return base.GetFormattedMessage(updatedInfo);
    }
}
<target
  xsi:type="FileTargetEx"
  name="file"
  filename="${specialfolder:folder=commonapplicationdata}/company gmbh/${appname}/logs/${appname}.log"
  keepfileopen="false"
  archivefilename="${specialfolder:folder=commonapplicationdata}/company gmbh/${appname}/logs/${appname}_archive.{##}.zip"
  archivenumbering="dateandsequence"
  archiveevery="day"
  maxarchivefiles="50"
  archivedateformat="yyyy-mm-dd"
  archiveoldfileonstartup="true"
  createdirs="true"
  enablearchivefilecompression="true"
  encoding="utf-8"
  header="############################################## ${appname} log ##############################################"
  layout="${longdate}|${level:uppercase=true}|${logger}|${message}"
  archiveabovesize="10485760"/>