項目中不少時候要記錄業務日誌,實際上是能夠直接用日誌框架計入數據庫的.git
使用NLog並非只能將日誌主體插入數據庫,而是能夠根據實際狀況自定義任意列記入.很是方便.並且很容易實現github
下面是用NLog記錄業務日誌到數據庫數據庫
1.首先下載NLog的DLL,下載地址app
https://github.com/NLog/NLog框架
2.項目裏面引用NLog.dll異步
3.建立一個日誌工具類ide
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using NLog; 6 7 namespace LN.Tool 8 { 9 10 public class DBLog 11 { 12 readonly static Logger processLogger = LogManager.GetLogger("LnProcessDBLog"); 13 14 public static void Process(string key,string userName, string orderNo, string content, string keyWord) 15 { 16 LogEventInfo theEvent = new LogEventInfo(LogLevel.Info, "", content); 17 theEvent.Properties["RequestKey"] = key; 18 theEvent.Properties["UserName"] = userName; 19 theEvent.Properties["OrderNo"] = orderNo; 20 theEvent.Properties["KeyWord"] = keyWord; 21 processLogger.Log(theEvent); 22 } 23 24 public static void Flush() 25 { 26 LogManager.Flush(); 27 } 28 } 29 }
4.而後在 Global.asax的Application_End中加入 DBLog.Flush();用來在網站中止時,將日誌所有插入工具
1 protected void Application_End(object sender, EventArgs e) 2 { 3 DBLog.Flush(); 4 }
5.在網站根目錄添加NLog.config文件網站
1 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 autoReload="true"> 4 <targets> 5 <default-wrapper xsi:type="BufferingWrapper" bufferSize="100" FlushTimeout="10000"> 6 <wrapper-target xsi:type="AsyncWrapper"/> 7 </default-wrapper> 8 <!-- write logs to file --> 9 <target xsi:type="File" name="LnProcessDBLogTarget2" fileName="D://logs/${shortdate}.log" 10 layout="${longdate} ${event-properties:item=RequestKey} ${event-properties:item=UserName} ${event-properties:item=OrderNo} ${message} ${event-properties:item=KeyWord}" /> 11 <target xsi:type="Database" name="LnProcessDBLogTarget"> 12 <!-- SQL command to be executed for each entry --> 13 <commandText>INSERT INTO [LogEntries](RequestKey, UserName,OrderNo,[Content],KeyWord,LogTime,CTime) 14 VALUES(@RequestKey,@UserName,@OrderNo,@Content,@KeyWord,@LogTime,getdate())</commandText> 15 <!-- parameters for the command --> 16 <parameter name="@RequestKey" layout="${event-properties:item=RequestKey}" /> 17 <parameter name="@UserName" layout="${event-properties:item=UserName}" /> 18 <parameter name="@OrderNo" layout="${event-properties:item=OrderNo}" /> 19 <parameter name="@Content" layout=" ${message}" /> 20 <parameter name="@KeyWord" layout="${event-properties:item=KeyWord}" /> 21 <parameter name="@LogTime" layout="${longdate}" /> 22 23 <!-- connection string --> 24 <dbProvider>System.Data.SqlClient</dbProvider> 25 <connectionString>server=.\SQLEXPRESS;database=MyLogs;integrated security=sspi</connectionString> 26 </target> 27 </targets> 28 <rules> 29 <logger name="LnProcessDBLog" minlevel="Trace" writeTo="LnProcessDBLogTarget" final="true" /> 30 <logger name="*" minlevel="Trace" writeTo="LnProcessDBLogTarget2" /> 31 </rules> 32 </nlog>
6.數據庫建立表spa
1 CREATE TABLE [dbo].[LogEntries]( 2 [Id] [bigint] IDENTITY(1,1) NOT NULL, 3 [RequestKey] [varchar](50) NOT NULL, 4 [UserName] [varchar](50) NOT NULL, 5 [OrderNo] [varchar](50) NOT NULL, 6 [Content] [varchar](5000) NOT NULL, 7 [KeyWord] [varchar](500) NOT NULL, 8 [LogTime] [datetime2](7) NOT NULL, 9 [CTime] [datetime] NOT NULL, 10 CONSTRAINT [PK_LogEntries] PRIMARY KEY CLUSTERED 11 ( 12 [Id] ASC 13 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 14 ) ON [PRIMARY] 15 16 GO
這樣就大功告成了,在插入日誌的地方寫就能夠了
DBLog.Process(key, "lnUserName", "O001OrderNo", key + "執行" + DateTime.Now, "K1KeyWord");
其中RequestKey等參數能夠根據實際狀況本身定義
1 <default-wrapper xsi:type="BufferingWrapper" bufferSize="100" FlushTimeout="10000"> 2 <wrapper-target xsi:type="AsyncWrapper"/> 3 </default-wrapper>
這幾句是用來設置如何插入的
<wrapper-target xsi:type="AsyncWrapper"/>表示異步插入