简体   繁体   English

nlog数据库目标参数布局错误

[英]nlog database target parameters layout error

I'm trying to use nlog database target. 我正在尝试使用nlog数据库目标。 First it doesn't work if I don't create the database manually. 首先,如果我不手动创建数据库,它将无法正常工作。 The other and more important thing, I can't log. 另外一件更重要的事情是,我无法登录。 I get errors about my parameter layout. 我收到有关参数布局的错误。 Out put from nlog internal log: 从nlog内部日志输出:

2016-11-12 18:45:49.0479 Debug Setting 'DatabaseParameterInfo.layout' to '${logged}'
2016-11-12 18:45:49.0744 Warn Error when setting property 'Layout' on 'NLog.Targets.DatabaseParameterInfo' Exception: System.ArgumentException: LayoutRenderer cannot be found: 'logged'
   at NLog.Config.Factory`2.CreateInstance(String name)
   at NLog.Layouts.LayoutParser.ParseLayoutRenderer(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr)
   at NLog.Layouts.LayoutParser.CompileLayout(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr, Boolean isNested, String& text)
   at NLog.Layouts.SimpleLayout.set_Text(String value)
   at NLog.Internal.PropertyHelper.TryNLogSpecificConversion(Type propertyType, String value, Object& newValue, ConfigurationItemFactory configurationItemFactory)
   at NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
2016-11-12 18:45:49.0909 Error Error in Parsing Configuration File. Exception: NLog.NLogConfigurationException: Exception occurred when loading configuration from C:\Github\private\WeBees\WeBees Client\WeBees.Server\NLog.config ---> NLog.NLogConfigurationException: Error when setting property 'Layout' on NLog.Targets.DatabaseParameterInfo ---> System.ArgumentException: LayoutRenderer cannot be found: 'logged'
   at NLog.Config.Factory`2.CreateInstance(String name)
   at NLog.Layouts.LayoutParser.ParseLayoutRenderer(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr)
   at NLog.Layouts.LayoutParser.CompileLayout(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr, Boolean isNested, String& text)
   at NLog.Layouts.SimpleLayout.set_Text(String value)
   at NLog.Internal.PropertyHelper.TryNLogSpecificConversion(Type propertyType, String value, Object& newValue, ConfigurationItemFactory configurationItemFactory)
   at NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
   --- End of inner exception stack trace ---
   at NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
   at NLog.Config.XmlLoggingConfiguration.ConfigureObjectFromAttributes(Object targetObject, NLogXmlElement element, Boolean ignoreType)
   at NLog.Config.XmlLoggingConfiguration.AddArrayItemFromElement(Object o, NLogXmlElement element)
   at NLog.Config.XmlLoggingConfiguration.SetPropertyFromElement(Object o, NLogXmlElement element)
   at NLog.Config.XmlLoggingConfiguration.ParseTargetElement(Target target, NLogXmlElement targetElement)
   at NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement)
   at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
   at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String filePath, Boolean autoReloadDefault)
   at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
   --- End of inner exception stack trace ---

My nlog.config 我的nlog.config

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      internalLogFile="C:\Github\private\WeBees\WeBees Client\WeBees.Server\Logs\nlog.txt" internalLogLevel="Trace">
  <targets>
    <target name="logfile" xsi:type="File" fileName="C:\Github\private\WeBees\WeBees Client\WeBees.Server\Logs\log.txt" />
    <target xsi:type="ColoredConsole" name="console" />
    <target name="database" xsi:type="Database">
      <!--
  Remarks:
    The appsetting layouts require the NLog.Extended assembly.
    The aspnet-* layouts require the NLog.Web assembly.
    The Application value is determined by an AppName appSetting in Web.config.
    The "NLogDb" connection string determines the database that NLog write to.
    The create dbo.Log script in the comment below must be manually executed.
  -->

      <connectionStringName>dev</connectionStringName>

      <!--
  SET ANSI_NULLS ON
  SET QUOTED_IDENTIFIER ON
  CREATE TABLE [dbo].[Log] (
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [Application] [nvarchar](50) NOT NULL,
      [Logged] [datetime] NOT NULL
    CONSTRAINT [PK_dbo.Log] PRIMARY KEY CLUSTERED ([Id] ASC)
      WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  ) ON [PRIMARY]
  -->

      <commandText>
        insert into dbo.Log (
        Application, Logged
        ) values (
        @Application, @Logged
        );
      </commandText>

      <parameter name="@application" layout="dadasda" />
      <parameter name="@logged" layout="${logged}" />
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="logfile,console" />
  </rules>
</nlog>

If I create the table manually and put constants in layout like in 'application' parameter it works. 如果我手动创建表并将常量放在“ application”参数中的布局中,则它会起作用。

I followed the documentation but just can't make it work. 我遵循了文档,但是无法正常工作。 I have NLog & NLog.Web extension installed. 我安装了NLog和NLog.Web扩展。

Thanks in advance! 提前致谢!

As the error message suggests, you should define ${logged} , eg in your config file: 如错误消息所建议的那样,您应该在配置文件中定义${logged}

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      internalLogFile="C:\Github\private\WeBees\WeBees Client\WeBees.Server\Logs\nlog.txt" internalLogLevel="Trace">

  <variable name="logged" value="my var" />
  <targets>

Or replace it with another layout renderer, eg ${message} , see the list of possible layout renderers 或将其替换为其他布局渲染器,例如${message} ,请参阅可能的布局渲染器列表

About the creation of the table, NLog will only create the table if you provide an install query: 关于表的创建,仅当您提供安装查询时,NLog才会创建表:

<target xsi:type="Database" name="db">

  <!-- connection string -->
  <dbProvider>System.Data.SqlClient</dbProvider>
  <connectionString>server=.\SQLEXPRESS;database=MyLogs;integrated security=sspi</connectionString>

  <!-- commands to install database -->
  <install-command>
    <text>CREATE DATABASE MyLogs</text>
    <connectionString>server=.\SQLEXPRESS;database=master;integrated security=sspi</connectionString>
    <ignoreFailures>true</ignoreFailures>
  </install-command>

  <install-command>
    <text>
      CREATE TABLE LogEntries(
      id int primary key not null identity(1,1),
      TimeStamp datetime2,
      Message nvarchar(max),
      level nvarchar(10),
      logger nvarchar(128))
    </text>

and call install, c#: 并调用install,C#:

LogManager.Configuration.Install(new InstallationContext()); //install all the targets, e.g. database, eventlogger etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM