时间:2021-07-01 10:21:17 帮助过:2人阅读
if exists (select 1 from sysobjects where id = object_id(‘Log‘) and type = ‘U‘) drop table Log go /*==============================================================*/ /* Table: Log */ /*==============================================================*/ create table Log ( Id bigint identity, AddTime datetime not null, Thread nvarchar(100) not null, Level nvarchar(200) not null, Logger nvarchar(500) not null, Message nvarchar(3000) not null, Exception nvarchar(4000) null, constraint PK_LOG primary key (Id) ) go declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty ‘MS_Description‘, ‘Log4net日志表‘, ‘user‘, @CurrentUser, ‘table‘, ‘Log‘ go declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty ‘MS_Description‘, ‘日志主键‘, ‘user‘, @CurrentUser, ‘table‘, ‘Log‘, ‘column‘, ‘Id‘ go declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty ‘MS_Description‘, ‘添加时间‘, ‘user‘, @CurrentUser, ‘table‘, ‘Log‘, ‘column‘, ‘AddTime‘ go declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty ‘MS_Description‘, ‘所在线程‘, ‘user‘, @CurrentUser, ‘table‘, ‘Log‘, ‘column‘, ‘Thread‘ go declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty ‘MS_Description‘, ‘级别‘, ‘user‘, @CurrentUser, ‘table‘, ‘Log‘, ‘column‘, ‘Level‘ go declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty ‘MS_Description‘, ‘拥有者‘, ‘user‘, @CurrentUser, ‘table‘, ‘Log‘, ‘column‘, ‘Logger‘ go declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty ‘MS_Description‘, ‘日志信息‘, ‘user‘, @CurrentUser, ‘table‘, ‘Log‘, ‘column‘, ‘Message‘ go declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty ‘MS_Description‘, ‘异常信息‘, ‘user‘, @CurrentUser, ‘table‘, ‘Log‘, ‘column‘, ‘Exception‘ go
04 在log4netdemo主项目根目录添加一个Log4net.config的配置文件
<?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender"> <bufferSize value="1" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <!--数据库连接字符串--> <connectionString value="DATABASE=Log4NetDB;SERVER=.;UID=sa;PWD=pwdpwd;Connect Timeout=15;" /> <commandText value="INSERT INTO Log ([AddTime],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" /> <parameter> <parameterName value="@log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout" /> </parameter> <parameter> <parameterName value="@thread" /> <dbType value="String" /> <size value="100" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%t" /> </layout> </parameter> <parameter> <parameterName value="@log_level" /> <dbType value="String" /> <size value="200" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%p" /> </layout> </parameter> <parameter> <parameterName value="@logger" /> <dbType value="String" /> <size value="500" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%logger" /> </layout> </parameter> <parameter> <parameterName value="@message" /> <dbType value="String" /> <size value="3000" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%m" /> </layout> </parameter> <parameter> <parameterName value="@exception" /> <dbType value="String" /> <size value="4000" /> <layout type="log4net.Layout.ExceptionLayout" /> </parameter> </appender> <root> <level value="WARN"/> <level value="INFO"/> <level value="DEBUG"/> <level value="FINE"/> <appender-ref ref="ADONetAppender" /> </root> <logger name="iNotes"> <level value="WARN"/> <level value="INFO"/> <level value="DEBUG"/> <level value="FINE"/> <appender-ref ref="ADONetAppender"/> </logger> <logger name="StellaLogger"> <level value="ALL"/> <appender-ref ref="AdoNetAppender" /> </logger> </log4net> </configuration>
05 在主项目中的AssemblyInfo.cs 添加
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
06 使用的时候,需要先实例化日志对象 比如在page里面
private static log4net.ILog logHelper = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); protected void Page_Load(object sender, EventArgs e) { //项目中调用使用日志 logHelper.Info("hehe"); }
在common里面
private static log4net.ILog logHelper = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public static void ComHelperDo() { int i = 0; try { int b = 3 / i; } catch (Exception ex) { logHelper.Error("发现异常", ex); } }在business里
private static log4net.ILog logHelper = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public static void BusHelperDo() { logHelper.Error("错误信息"); }
最后数据库里就添加成功了
Log4Net的应用教程之保存日志到数据库中
标签: