VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之NLog组件(2)

else if (operatorLogModel.LogLevel == NLog.LogLevel.Warn) 180 level = LogLevel.Warn; 181 else if (operatorLogModel.LogLevel == NLog.LogLevel.Error) 182 level = LogLevel.Error; 183 else if (operatorLogModel.LogLevel == NLog.LogLevel.Fatal) 184 level = LogLevel.Fatal; 185 186 if (operatorLogModel.LogMessage.Length > 3000) 187 { 188 operatorLogModel.LogMessage = operatorLogModel.LogMessage.Substring(0, 3000); 189 } 190 lei.Properties["Id"] = Guid.NewGuid().ToString("D"); 191 lei.Properties["AppName"] = operatorLogModel.AppName; 192 lei.Properties["ModuleName"] = operatorLogModel.ModuleName; 193 lei.Properties["ProcName"] = operatorLogModel.ProcName; 194 lei.Properties["OperationType"] = operatorLogModel.OperationType; 195 lei.Properties["Logger"] = operatorLogModel.Logger; 196 lei.Properties["LogMessage"] = operatorLogModel.LogMessage; 197 lei.Properties["IP"] = operatorLogModel.IP ?? DefaultIP; 198 lei.Properties["Longdate"] = operatorLogModel.Longdate; 199 lei.Properties["UserName"] = operatorLogModel.UserName ?? DefaultUser; 200 lei.Properties["Createdate"] = operatorLogModel.Createdate; 201 lei.Level = operatorLogModel.LogLevel; 202 _logger.Log(level, lei); 203 } 204 #endregion 205 }
复制代码

对操作类型进行简单封装,也可以自定义:

复制代码
 1 /// <summary>
 2     /// 操作类型枚举
 3     /// </summary>
 4     public enum OperationType
 5     {
 6         /// <summary>
 7         /// 保存或添加
 8         /// </summary>
 9         [System.ComponentModel.Description("添加")]
10         ADD,
11         /// <summary>
12         /// 更新
13         /// </summary>
14         [System.ComponentModel.Description("更新")]
15         UPDATE,
16         /// <summary>
17         /// 核销
18         /// </summary>
19         [System.ComponentModel.Description("核销")]
20         AUDIT,
21         /// <summary>
22         /// 查看
23         /// </summary>
24         [System.ComponentModel.Description("指派")]
25         ASSIGN,
26         /// <summary>
27         /// 删除
28         /// </summary>
29         [System.ComponentModel.Description("删除")]
30         DELETE,
31         /// <summary>
32         /// 读取/查询
33         /// </summary>
34         [System.ComponentModel.Description("查询")]
35         RETRIEVE,
36         /// <summary>
37         /// 登录
38         /// </summary>
39         [System.ComponentModel.Description("登录")]
40         LOGIN,
41         /// <summary>
42         /// 查看
43         /// </summary>
44         [System.ComponentModel.Description("查看")]
45         LOOK
46     }
复制代码

自定义类,主要用于绑定数据:

复制代码
 1 /// <summary>
 2     /// 操作日志类
 3     /// </summary>
 4     public class OperatorLogModel
 5     {
 6         /// <summary>
 7         /// 自增主键ID
 8         /// </summary>
 9         public string Id { get; set; }
10         /// <summary>
11         /// 一级菜单
12         /// </summary>
13         public string AppName { get; set; }
14         /// <summary>
15         /// 二级菜单
16         /// </summary>
17         public string ModuleName { get; set; }
18         /// <summary>
19         /// 本级菜单
20         /// </summary>
21         public string ProcName { get; set; }
22         /// <summary>
23         /// 操作类型
24         /// </summary>
25         public int OperationType { get; set; }
26         /// <summary>
27         /// 日志文件
28         /// </summary>
29         public string Logger { get; set; }
30         /// <summary>
31         /// 日志信息
32         /// </summary>
33         public string LogMessage { get; set; }
34         /// <summary>
35         /// IP地址
36         /// </summary>
37         public string IP { get; set; }
38         /// <summary>
39         /// 记录时间
40         /// </summary>
41         public string Longdate { get; set; }
42         /// <summary>
43         /// 用户名称
44         /// </summary>
45         public string UserName { get; set; }
46         /// <summary>
47         /// 日志级别
48         /// </summary>
49         public NLog.LogLevel LogLevel { get; set; }
50         /// <summary>
51         /// 创建时间
52         /// </summary>
53         public DateTime Createdate { get; set; }
54     }
复制代码

创建数据库表,字段可以自定义,此处用的是SQL Server:

复制代码
 1 CREATE TABLE [dbo].[OperatorLog](
 2     [Id] [varchar](60) NOT NULL,
 3     [AppName] [varchar](20) NOT NULL,
 4     [ModuleName] [varchar](30) NOT NULL,
 5     [ProcName] [varchar](30) NOT NULL,
 6     [OperationType] [int] NOT NULL,
 7     [Logger] [varchar](500) NOT NULL,
 8     [LogMessage] [varchar](3000) NOT NULL,
 9     [IP] [varchar](32) NOT NULL,
10     [UserName] [varchar](36) NOT NULL,
11     [Createdate] [datetime] NOT NULL,
12     [LogLevel] [varchar](12) NOT NULL,
13  CONSTRAINT [PK_OperatorLog] PRIMARY KEY CLUSTERED 
14 (
15     [Id] ASC
16 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
17 ) ON [PRIMARY]
18 
19 GO
20 
21 SET ANSI_PADDING OFF
22 GO
23 
24 ALTER TABLE [dbo].[OperatorLog] ADD  CONSTRAINT [DF_OperatorLog_Id]  DEFAULT ('') FOR [Id]
25 GO
26 
27 ALTER TABLE [dbo].[OperatorLog] ADD  CONSTRAINT [DF_OperatorLog_AppName]  DEFAULT ('') FOR [AppName]
28 GO
29 
30 ALTER TABLE [dbo].[OperatorLog] ADD  CONSTRAINT [DF_OperatorLog_ModuleName]  DEFAULT ('') FOR [ModuleName]
31 GO
32 
33 ALTER TABLE [dbo].[OperatorLog] ADD  CONSTRAINT [DF_OperatorLog_ProcName]  DEFAULT ('') FOR [ProcName]
34 GO
35 
36 ALTER TABLE [dbo].[OperatorLog] ADD  CONSTRAINT [DF_OperatorLog_OperationType]  DEFAULT ((0)) FOR [OperationType]
37 GO
38 
39 ALTER TABLE [dbo].[OperatorLog] ADD  CONSTRAINT [DF_OperatorLog_Logger]  DEFAULT ('') FOR [Logger]
40 GO
41 
42 ALTER TABLE [dbo].[OperatorLog] ADD  CONSTRAINT [DF_OperatorLog_LogMessage]  DEFAULT ('') FOR [LogMessage]
43 GO
44 
45 ALTER TABLE [dbo].[OperatorLog] ADD  CONSTRAINT [DF_OperatorLog_IP]  DEFAULT ('') FOR [IP]
46 GO
47 
48 ALTER TABLE [dbo].[OperatorLog] ADD  CONSTRAINT [DF_OperatorLog_UserName]  DEFAULT ('') FOR [UserName]
49 GO
50 
51 ALTER TABLE [dbo].[OperatorLog] ADD  CONSTRAINT [DF__OperatorL__creat__02084FDA]  DEFAULT (getdate()) FOR [Createdate]
52 GO
53 
54 ALTER TABLE [dbo].[OperatorLog] ADD  CONSTRAINT [DF_OperatorLog_LogLevel]  DEFAULT ('') FOR [LogLevel]
55 GO
56 
57 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'日志表主键ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OperatorLog', @level2type=N'COLUMN'
      



  

相关教程