1
是否有针对所有可能字段的数据库目标日志表的create语句?我通过猜测创建了一个,我可以检查源代码,但如果通用SQL可用,它会很方便。我搜索了StackOverflow和NLog站点,但我发现的SQL已过时且包含不正确的字段类型。
2
如果您正在从nlog.config文件配置数据库目标,那么如何以编程方式设置连接字符串?类似的东西:
Logger dbLogger = LogManager.GetLogger(“dbLogger”); DatabaseTarget t = dbLogger.GetDatabaseTarget; t.ConnectionString =“...”;
在application_start。
提前致谢。
答案 0 :(得分:0)
以下示例对您有帮助吗?我在nlog网站上找到了这个。你试过这个吗?
<target xsi:type="Database" name="db">
<!-- SQL command to be executed for each entry -->
<commandText>INSERT INTO [LogEntries](TimeStamp, Message, Level, Logger) VALUES(getutcdate(), @msg, @level, @logger)</commandText>
<!-- parameters for the command -->
<parameter name="@msg" layout="${message}" />
<parameter name="@level" layout="${level}" />
<parameter name="@logger" layout="${logger}" />
<!-- 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>
</install-command>
<!-- commands to uninstall database -->
<uninstall-command>
<text>DROP DATABASE MyLogs</text>
<connectionString>server=.\SQLEXPRESS;database=master;integrated security=sspi</connectionString>
<ignoreFailures>true</ignoreFailures>
</uninstall-command>
</target>