为什么我不能通过 NLog 将日志记录保存到数据库? [英] Why I can't save logging to database via NLog?
本文介绍了为什么我不能通过 NLog 将日志记录保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么我不能通过 NLog 将日志保存到数据库?然后我保存到文件工作正常,但没有保存到数据库.我的 nLog.config:
Why I can't save logging to database via NLog? Then I save to file is working perfectly, but to database not. My nLog.config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="alllogs" xsi:type="File" fileName="C:\Users\Artyrik\Desktop\RVT\RVT_Administrator\RVT_Administrator\file.txt" />
<target name="database" xsi:type="Database" connectionString="Server=DESKTOP-GDI15RS\\SQLEXPRESS;Database=RVTLogs;Trusted_Connection=True;MultipleActiveResultSets=true"
commandText="insert into dbo.Administrator (
MachineName, Logged, Level, Message,
Logger, Callsite, Exception
) values (
@MachineName, @Logged, @Level, @Message,
@Logger, @Callsite, @Exception
);">
<parameter name="@MachineName" layout="${machinename}" />
<parameter name="@Logged" layout="${date}" />
<parameter name="@Level" layout="${level}" />
<parameter name="@Message" layout="${message}" />
<parameter name="@Logger" layout="${logger}" />
<parameter name="@Callsite" layout="${callsite}" />
<parameter name="@Exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<!--<logger name="Admin" minlevel="Trace" writeTo="alllogs" />
<logger name="User" minlevel="Trace" writeTo="alllogs" />-->
<logger name="AdminLog" minlevel="Trace" writeTo="database" />
</rules>
</nlog>
在类中实现:
```
private static Logger _nLog = LogManager.GetLogger("AdminLog");
_nLog.Info(data.IP + " tried to auth.");```
推荐答案
您需要将 internalLogFile 参数添加到您的 nlog 标签中.如果发生错误,您将拥有一个内部日志并查看您的配置有什么问题:
You need to add internalLogFile parameter to your nlog tag. You will have an internal log and see what is wrong with your configuration if an error occured :
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Info"
internalLogFile="c:\temp\internal-nlog.txt">
这篇关于为什么我不能通过 NLog 将日志记录保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文