NLog 正在写入文件,但不写入数据库表
本文关键字:数据库 文件 NLog | 更新日期: 2023-09-27 18:37:11
这是我的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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:'temp'nlog-internal.log" >
<variable name="myvar" value="myvalue"/>
<targets>
<target name="database" xsi:type="Database"
dbUserName="internwebuser"
dbProvider="System.Data.SqlClient"
connectionStringName="SECURE"
connectionString="Data Source=sqlservtest.starledger.com;Initial Catalog=SECURE;User ID=webuser;Password=password"
commandText="insert into dbo.tracelog (custom_message, excep_message, insertdate)
values
(@custom_message, @excep_message, @insertdate);" >
<parameter name="@custom_message" layout="${exception}" />
<parameter name="@excep_message" layout="${exception:tostring}" />
<parameter name="@insertdate" layout="${date}" />
</target>
<target name="file" xsi:type="File"
layout="${longdate} ${uppercase:${level}} ${callsite} from Line: ${callsite-linenumber} ${newline}
${exception} ${newline}
${exception:tostring} ${newline}"
fileName="${basedir}/logs/${shortdate}-${level}.log" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
<logger name="*" minlevel="Debug" writeTo="database" />
</rules>
</nlog>
我的控制器看起来像这样:
public class ReportController : Controller
{
private static Logger logger = LogManager.GetCurrentClassLogger();
public ActionResult Index()
{
try
{
<!-- some statement -->
}
catch (Exception e)
{
logger.Error(e);
}
return View();
}
}
此代码完美地写入文件,但数据库表仍为空。此外,在我的文件中,当我使用 StreamWriter
执行此操作并创建一个文件而不是使用 NLog 时,应用程序会同时将 2/3 种错误写入文件。但是 NLog 只写最后一个错误。
例如:
在StreamWriter
的情况下,错误是:
Exception Name:ExecuteReader requires an open and available Connection. The connection's current state is closed.
Exception Name:Object reference not set to an instance of an object.
但是在NLog的情况下,我只得到:
Exception Name:Object reference not set to an instance of an object.
and the first one is missed out.
我的StreamWriter
方法如下所示:
DateTime today = DateTime.Now;
string path = Directory.GetCurrentDirectory();
string fileName = "error-log-" + today.Date.ToString("MM-dd-yyyy") + ".txt";
var file = Path.Combine(path, fileName);
StreamWriter log;
if (!File.Exists(file))
{
log = new StreamWriter(file);
}
else
{
log = File.AppendText(file);
}
// Write to the file:
log.WriteLine("Data Time:" + DateTime.Now);
log.WriteLine("Exception Name:" + sExceptionName);
log.WriteLine("Event Name:" + sEventName);
log.WriteLine("Error Line No.:" + nErrorLineNo);
// Close the stream:
log.Close();
我将该方法传递到控制器的catch
部分。
我在这里的主要问题是写入数据库表,尽管我也希望帮助使用 Nlog 写入所有错误,而不仅仅是最后一个错误。
请启用内部日志记录以获取更多信息
我遇到了同样的问题。为了找到确切的问题,我在nlog标签中添加了<nlog internalLogFile="c:'DemoLogs'nlog-internal.txt" and internalLogLevel="Error">
。它将确切的 proplem 写入内部日志文件(nlog-internal.txt)
就我而言Could not load file or assembly 'System.Data.SqlClient
通过安装所需的nuget包来解决。