欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.NET4.7使用NLog記錄日志到數(shù)據(jù)庫表

 更新時(shí)間:2025年06月20日 16:16:01   作者:MartinYangHJ  
這篇文章主要為大家詳細(xì)介紹了.NET4.7如何使用NLog實(shí)現(xiàn)記錄日志到數(shù)據(jù)庫表,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1. 首先安裝必要的NuGet包

在項(xiàng)目中安裝以下NuGet包:

  • NLog
  • NLog.Config (可選,用于自動(dòng)生成配置文件)

相應(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文件

在項(xiàng)目中添加或修改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ù)庫目標(biāo) -->
    <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ù)庫目標(biāo) -->
    <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ù)庫中有對(duì)應(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)文章

最新評(píng)論