项目中很多时候要记录业务日志,其实是可以直接用日志框架计入数据库的.
使用NLog并不是只能将日志主体插入数据库,而是可以根据实际情况自定义任意列记入.非常方便.而且很容易实现
下面是用NLog记录业务日志到数据库
1.首先下载NLog的DLL,下载地址
https://github.com/NLog/NLog
2.项目里面引用NLog.dll
3.创建一个日志工具类
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 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 13 <commandText>INSERT INTO [LogEntries](RequestKey, UserName,OrderNo,[Content],KeyWord,LogTime,CTime) 14 VALUES(@RequestKey,@UserName,@OrderNo,@Content,@KeyWord,@LogTime,getdate())commandText> 15 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 24 <dbProvider>System.Data.SqlClientdbProvider> 25 <connectionString>server=.\SQLEXPRESS;database=MyLogs;integrated security=sspiconnectionString> 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.数据库创建表
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>
这几句是用来设置如何插入的
表示异步插入