【问题标题】:NLog Database Not Being Created未创建 NLog 数据库
【发布时间】:2021-08-20 13:16:26
【问题描述】:

我是第一次设置 nlog,并且有多个位置可以记录...这是我在 nlog 中的设置,但从未创建表?

<target xsi:type="Database"
       name="DatabaseTarget"
       keepConnection="true"
       commandType="Text"
       dbDatabase="IISLogging"
       dbUserName="user"
       dbPassword="password"
       dbHost="111.111.11.1"
       commandText="INSERT INTO [Log] (Logged,Level,LogMessage,Exception,StackTrace,Path,Json,Ip) 
                    VALUES (@Logged, @Level, @Message, @Exception, @StackTrace,@Path,@Json,@Ip)">

【问题讨论】:

    标签: c# nlog


    【解决方案1】:

    假设nlog.config 正确填写了您的数据库提供者的正确值,您可以使用数据库目标的安装功能。

    LogManager.Configuration.Install(new InstallationContext());
    

    安装数据库目标示例

    <target xsi:type="Database" name="db"
            connectionStringName="LoggingDatabase">
        <install-command>
            <text>
                <!--
                NOTE: call LogManager.Configuration.Install(new InstallationContext()); 
                      to execute this query.
                -->
                CREATE TABLE ${var:logTableName} (
                Id bigint primary key not null identity(1,1),
                Logged datetime2,
                Level nvarchar(10),
                LogMessage nvarchar(max),
                MessageUid uniqueidentifier,
                MessagePartUid uniqueidentifier,
                MessagePartDataUid uniqueidentifier,
                )
            </text>
            <ignoreFailures>false</ignoreFailures>
        </install-command>
    

    https://github.com/NLog/NLog/wiki/Installing-targets

    另外,请尝试使用内部记录器进行故障排除

    <nlog internalLogLevel="Debug"
          internalLogFile="c:\temp\nlog-internal.txt"
          internalLogToConsole="true"
          throwConfigExceptions="true">
        <targets>
    

    【讨论】:

    • 这是我的困惑。在我的Startup.cs 我有我的Configure 方法LogManager.Configuration.Install(new InstallationContext()); 我已经在变量namedbDatabase 中指定了DbName,并且我已经将此信息输入到文件Nlog.Dev.configNlog.Development.config , Nlog.Prod.config
    • 如果问题不是加载无效的NLog.config,则激活NLog InternalLogger
    猜你喜欢
    • 2012-08-23
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多