.NET4.7使用NLog記錄日志到數(shù)據(jù)庫表
1. 首先安裝必要的NuGet包
在項目中安裝以下NuGet包:
- NLog
- NLog.Config (可選,用于自動生成配置文件)
相應(yīng)的數(shù)據(jù)庫提供程序(如System.Data.SqlClient for SQL Server)
Install-Package NLog Install-Package NLog.Config Install-Package System.Data.SqlClient
2. 配置NLog.config文件
在項目中添加或修改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"
autoReload="true"
internalLogLevel="Info"
internalLogFile="c:\temp\nlog-internal.log">
<extensions>
<add assembly="NLog" />
</extensions>
<targets>
<!-- 數(shù)據(jù)庫目標 -->
<target name="database"
xsi:type="Database"
connectionString="YourConnectionStringHere"
commandText="INSERT INTO LoginLogs(ID, LoginName, Message, CreateTime) VALUES(@ID, @LoginName, @Message, @CreateTime)">
<parameter name="@ID" layout="${guid}" />
<parameter name="@LoginName" layout="${event-properties:item=LoginName}" />
<parameter name="@Message" layout="${message}" />
<parameter name="@CreateTime" layout="${date:format=yyyy-MM-dd HH\:mm\:ss}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="database" />
</rules>
</nlog>如果需要使用異步記錄日志的話,配置如下:
<?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"
autoReload="true"
internalLogLevel="Info"
internalLogFile="c:\temp\nlog-internal.log">
<extensions>
<add assembly="NLog" />
</extensions>
<targets async="true"> <!-- 啟用全局異步 -->
<!-- 異步數(shù)據(jù)庫目標 -->
<target name="asyncDatabase" xsi:type="AsyncWrapper"
queueLimit="10000"
overflowAction="Discard">
<target xsi:type="Database"
connectionString="YourConnectionStringHere"
commandText="INSERT INTO LoginLogs(ID, LoginName, Message, CreateTime) VALUES(@ID, @LoginName, @Message, @CreateTime)">
<parameter name="@ID" layout="${guid}" />
<parameter name="@LoginName" layout="${event-properties:item=LoginName}" />
<parameter name="@Message" layout="${message}" />
<parameter name="@CreateTime" layout="${date:format=yyyy-MM-dd HH\:mm\:ss}" />
</target>
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="asyncDatabase" />
</rules>
</nlog>3. 創(chuàng)建數(shù)據(jù)庫表
確保你的數(shù)據(jù)庫中有對應(yīng)的表結(jié)構(gòu):
CREATE TABLE LoginLogs (
ID UNIQUEIDENTIFIER PRIMARY KEY,
LoginName NVARCHAR(100),
Message NVARCHAR(MAX),
CreateTime DATETIME
)
4. 在代碼中使用NLog記錄登錄日志
using NLog;
public class LoginService
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public void LogLoginAttempt(string loginName, string message, bool isSuccess)
{
var logEvent = new LogEventInfo
{
Level = isSuccess ? LogLevel.Info : LogLevel.Warn,
Message = message
};
logEvent.Properties["LoginName"] = loginName;
Logger.Log(logEvent);
}
}5. 使用示例
var loginService = new LoginService();
// 成功登錄
loginService.LogLoginAttempt("john.doe", "User logged in successfully", true);
// 失敗登錄
loginService.LogLoginAttempt("john.doe", "Invalid password", false);
到此這篇關(guān)于.NET4.7使用NLog記錄日志到數(shù)據(jù)庫表的文章就介紹到這了,更多相關(guān).NET NLog記錄日志到數(shù)據(jù)庫表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
asp.net 中靜態(tài)方法和動態(tài)方法調(diào)用的區(qū)別實例分析
動態(tài)方法,在使用時需要先創(chuàng)建實例,才能調(diào)用實例方法,而靜態(tài)方法則不需要,直接使用即可。2013-06-06
.NET程序啟動就報錯如何截獲初期化時的問題json(最新推薦)
這篇文章主要介紹了.NET程序啟動就報錯,如何截獲初期化時的問題json,本文通過小案例給大家詳細講解,感興趣的朋友一起看看吧2025-05-05
在.NET Core類庫中使用EF Core遷移數(shù)據(jù)庫到SQL Server的方法
下面小編就為大家分享一篇在.NET Core類庫中使用EF Core遷移數(shù)據(jù)庫到SQL Server的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12

