【问题标题】:NLog - DDL for Log Table and Setting the ConnectionString ProgrammaticallyNLog - 日志表的 DDL 和以编程方式设置 ConnectionString
【发布时间】:2011-03-10 09:16:14
【问题描述】:
1:
对于记录在某处的所有可能字段,是否有数据库目标日志表的创建语句?我通过猜测创建了一个,我可以检查源代码,但如果通用 SQL 可用,它会很方便。我搜索了 StackOverflow 和 NLog 站点,但发现的 SQL 已过时并且包含不正确的字段类型。
2:
如果您从 nlog.config 文件配置数据库目标,您如何以编程方式设置连接字符串?比如:
记录器 dbLogger = LogManager.GetLogger("dbLogger");
数据库目标 t = dbLogger.GetDatabaseTarget;
t.ConnectionString = "...";
在 application_start 中。
提前致谢。
【问题讨论】:
标签:
asp.net
sql-server
logging
nlog
【解决方案1】:
以下示例对您有帮助吗?我在 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>