.NET 4.8控制台应用NLog写入MSSQL无日志输出问题
问题:.NET 4.8控制台应用使用NLog写入MSSQL数据库无日志记录
应用代码
private static Logger nLogger = LogManager.GetLogger("myLogger"); static void Main() { nLogger.Info("ConsoleApp Started"); // MAIN code of application nLogger.Info("ConsoleApp Ended"); LogManager.Flush(); }
app.config配置
<configuration> <configSections> <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="ConsoleApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> </startup> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true"> <!-- the targets to write to --> <targets async="true"> <target name="db" xsi:type="Database" connectionString="server=127.0.0.1\test;Database=TicketDB_Test;user id=USER;password=PASSWORD" commandType="StoredProcedure" commandText="[dbo].[NLog_AddEntry]"> <parameter name="@machineName" layout="${machinename}" /> <parameter name="@level" layout="${level}"/> <parameter name="@message" layout="${message}"/> <parameter name="@logger" layout="${processname}" /> <parameter name="@loggertype" layout="${literal:text=ConsoleApplication}" /> <parameter name="@properties" layout="${all-event-properties:separator=|}" /> <parameter name="@callsite" layout="${callsite}"/> <parameter name="@linenumber" layout="${callsite-linenumber}" /> <parameter name="@exception" layout="${exception:tostring}"/> <parameter name="@stackTrace" layout="${stacktrace}"/> </target> </targets> <!-- rules to map from logger name to target --> <rules> <logger name="myLogger" minlevel="Trace" writeTo="db" /> </rules> </nlog> <applicationSettings> <ConsoleApp.Properties.Settings> //... </ConsoleApp.Properties.Settings> </applicationSettings> </configuration>
环境信息
- .NET Framework 4.8控制台应用
- NLog 5.2.4、NLog.Database 5.2.4
- 存储过程
NLog_AddEntry测试正常,但运行应用后数据库无日志写入
排查步骤
- 处理异步目标的刷新等待:因为配置中开启了
async="true",默认的LogManager.Flush()不会等待异步队列处理完成,修改为LogManager.Flush(TimeSpan.FromSeconds(5)),给足够时间让异步任务执行完毕。 - 验证数据库连接字符串:确认实例名(
127.0.0.1\test)、用户名、密码正确,数据库服务正常运行,且该用户拥有执行存储过程NLog_AddEntry及写入目标日志表的权限。 - 开启NLog内部日志排查错误:在
<nlog>节点中添加内部日志配置,比如:
运行应用后查看生成的<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogFile="nlog-internal.log" internalLogLevel="Warn">nlog-internal.log,里面会记录连接失败、参数不匹配、存储过程调用错误等关键信息。 - 检查存储过程参数匹配:确认
NLog_AddEntry的参数名、数据类型、可空性与NLog配置中的参数完全一致。比如@linenumber是否为INT类型,@loggertype的字符串长度是否足够容纳ConsoleApplication,避免因参数类型不匹配导致存储过程执行失败。 - 临时禁用异步写入:将
<targets async="true">改为<targets>,切换为同步写入模式,测试是否能正常写入数据库,排除异步队列的问题。 - 确认NLog配置加载状态:在
Main方法开头添加代码,验证配置是否正确加载:var config = LogManager.Configuration; if (config == null) { Console.WriteLine("NLog配置未加载"); } else { Console.WriteLine($"加载的Targets数量:{config.AllTargets.Count}"); Console.WriteLine($"加载的Rules数量:{config.LoggingRules.Count}"); } - 检查日志级别过滤:确认没有全局配置或代码逻辑覆盖了日志级别,比如是否存在
LogManager.GlobalThreshold被设置为高于Info的级别,导致日志被过滤。
内容的提问来源于stack exchange,提问作者Mych
相关产品推荐
相关产品推荐

