<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<!--定义文件存放位置-->
<file value="D:\log-file1.txt" />
<!-- Example using environment variables in params -->
<!-- <file value="${TMP}\log-file.txt" /> -->
<!--<sppendToFile value="true" />-->
<!-- An alternate output encoding can be specified -->
<!-- <encoding value="unicodeFFFE" /> -->
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd-HH:mm:ss" />
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明-->
<footer value="[Footer]--Test By Ring1981 " />
<!--输出格式-->
<conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" />
</layout>
</appender>
<logger name="LogToSqlite">
<level value="ERROR"/>
<appender-ref ref="ADONetAppender"/>
</logger>
<!--<logger name="LogToFile">
<level value="ALL"/>
<appender-ref ref="LogAllToFile"/>
</logger>-->
<!--定义输出到控制台命令行中-->
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!--定义输出到windows事件中-->
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!--定义输出到SqlServer 数据库中-->
<logger name="LogSystem">
<level value="ALL"/>
<appender-ref ref="AdoNetAppender_SqlServer" />
</logger>
<appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
<buffersize value="100" />
<bufferSize value="30" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<connectionString value="server=.\SQLEXPRESS;database=hyundai2;integrated security=false;persist security info=True;UID=sa;PWD=sa;Connect Timeout=15;"/>
<commandText value="INSERT INTO Log ([Date],[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.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}"/>
</parameter>
<parameter>
<parameterName value="@thread"/>
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout" value="%thread"/>
</parameter>
<parameter>
<parameterName value="@log_level"/>
<dbType value="String"/>
<size value="50"/>
<layout type="log4net.Layout.PatternLayout" value="%level"/>
</parameter>
<parameter>
<parameterName value="@logger"/>
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout" value="%logger"/>
</parameter>
<parameter>
<parameterName value="@message"/>
<dbType value="String"/>
<size value="4000"/>
<layout type="log4net.Layout.PatternLayout" value="%message"/>
</parameter>
<parameter>
<parameterName value="@exception"/>
<dbType value="String"/>
<size value="4000"/>
<layout type="log4net.Layout.PatternLayout" value="%exception"/>
</parameter>
</appender>
<!--定义日志的输出媒介,下面定义日志以四种方式输出。也可以下面的按照一种类型或其他类型输出。-->
<root>
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="EventLogAppender" />
<appender-ref ref="AdoNetAppender_Access" />
</root>
</log4net>
能记录到txt 文档。但是错误信息不能记录到数据。一直没有找到原因;
还有想请教下,错误日志。是不是try cacth 抛出的异常不能直接记录呢。必须是弹出错误页面,才能protected void Application_Error()这个方法记录下来呢。
5 个解决方案
#1
Global.asax 配置
新建的类,写入txt 或者数据库
这个是我写入到数据库的代码,一直不能写入数据库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace AutoSales.Web
{
public class Global : System.Web.HttpApplication
{
private static log4net.ILog LOG = log4net.LogManager.GetLogger("Log4net");
protected void Application_Start(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码
AppLog.Write("Web站点开始...", AppLog.LogMessageType.Info);
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
Exception ex = Server.GetLastError();
//LOG.Error("未处理异常", ex);
if (ex != null)
{
//写入错误日志
AppLog.Write("[Exception]:", AppLog.LogMessageType.Error, ex);
// 如果是 HttpException 异常 不处理, 显示403 404 页面
if (ex is HttpException)
{
HttpException httpEx = ex as HttpException;
if (httpEx.GetHttpCode() == 403 || httpEx.GetHttpCode() == 404)
return;
}
// Response.Redirect("~/Error.aspx");
}
//Exception ex = HttpContext.Current.Server.GetLastError();
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码
AppLog.Write("Web站点关闭...", AppLog.LogMessageType.Info);
}
}
}
新建的类,写入txt 或者数据库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using log4net;
using System.Reflection;
namespace AutoSales.Web
{
public class AppLog
{ /// <summary>
/// 日志处理
/// </summary
private AppLog() { }
//[assembly: log4net.Config.XmlConfigurator(Watch = true)]
private const string LOG_REPOSITORY = "Default"; // this should likely be set in the web config.
private static ILog m_log;
/// <summary>
/// 初始化日志系统
/// 在系统运行开始初始化
/// Global.asax Application_Start内
/// </summary>
///
public static void Init()
{
log4net.Config.XmlConfigurator.Configure();
}
/// <summary>
/// 写入日志
/// </summary>
/// <param name="message">日志信息</param>
/// <param name="messageType">日志类型</param>
public static void Write(string message, LogMessageType messageType)
{
DoLog(message, messageType, null, Type.GetType("System.Object"));
}
/// <summary>
/// 写入日志
/// </summary>
/// <param name="message">日志信息</param>
/// <param name="messageType">日志类型</param>
/// <param name="type"></param>
public static void Write(string message, LogMessageType messageType, Type type)
{
DoLog(message, messageType, null, type);
}
/// <summary>
/// 断言
/// </summary>
/// <param name="condition">条件</param>
/// <param name="message">日志信息</param>
/// <param name="type">日志类型</param>
public static void Assert(bool condition, string message, Type type)
{
if (condition == false)
Write(message, LogMessageType.Info);
}
/// <summary>
/// 保存日志
/// </summary>
/// <param name="message">日志信息</param>
/// <param name="messageType">日志类型</param>
/// <param name="ex">异常</param>
/// <param name="type">日志类型</param>
private static void DoLog(string message, LogMessageType messageType, Exception ex,
Type type)
{
m_log = LogManager.GetLogger(type);
switch (messageType)
{
case LogMessageType.Debug:
AppLog.m_log.Debug(message, ex);
break;
case LogMessageType.Info:
AppLog.m_log.Info(message, ex);
break;
case LogMessageType.Warn:
AppLog.m_log.Warn(message, ex);
break;
case LogMessageType.Error:
AppLog.m_log.Error(message, ex);
break;
case LogMessageType.Fatal:
AppLog.m_log.Fatal(message, ex);
break;
}
}
/// <summary>
/// 日志类型
/// </summary>
public enum LogMessageType
{
/// <summary>
/// 调试
/// </summary>
Debug,
/// <summary>
/// 信息
/// </summary>
Info,
/// <summary>
/// 警告
/// </summary>
Warn,
/// <summary>
/// 错误
/// </summary>
Error,
/// <summary>
/// 致命错误
/// </summary>
Fatal
}
}
}
这个是我写入到数据库的代码,一直不能写入数据库
#2
第一,无法记录数据库很有可能是数据库连接问题,先确定连接字符串;
第二,log4net可以在任何地方调用写日志方法,跟错误来源没有关系;
第二,log4net可以在任何地方调用写日志方法,跟错误来源没有关系;
#3
但是必须抛出异常。他才能检测到记录下来是嘛?
#1
Global.asax 配置
新建的类,写入txt 或者数据库
这个是我写入到数据库的代码,一直不能写入数据库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace AutoSales.Web
{
public class Global : System.Web.HttpApplication
{
private static log4net.ILog LOG = log4net.LogManager.GetLogger("Log4net");
protected void Application_Start(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码
AppLog.Write("Web站点开始...", AppLog.LogMessageType.Info);
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
Exception ex = Server.GetLastError();
//LOG.Error("未处理异常", ex);
if (ex != null)
{
//写入错误日志
AppLog.Write("[Exception]:", AppLog.LogMessageType.Error, ex);
// 如果是 HttpException 异常 不处理, 显示403 404 页面
if (ex is HttpException)
{
HttpException httpEx = ex as HttpException;
if (httpEx.GetHttpCode() == 403 || httpEx.GetHttpCode() == 404)
return;
}
// Response.Redirect("~/Error.aspx");
}
//Exception ex = HttpContext.Current.Server.GetLastError();
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码
AppLog.Write("Web站点关闭...", AppLog.LogMessageType.Info);
}
}
}
新建的类,写入txt 或者数据库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using log4net;
using System.Reflection;
namespace AutoSales.Web
{
public class AppLog
{ /// <summary>
/// 日志处理
/// </summary
private AppLog() { }
//[assembly: log4net.Config.XmlConfigurator(Watch = true)]
private const string LOG_REPOSITORY = "Default"; // this should likely be set in the web config.
private static ILog m_log;
/// <summary>
/// 初始化日志系统
/// 在系统运行开始初始化
/// Global.asax Application_Start内
/// </summary>
///
public static void Init()
{
log4net.Config.XmlConfigurator.Configure();
}
/// <summary>
/// 写入日志
/// </summary>
/// <param name="message">日志信息</param>
/// <param name="messageType">日志类型</param>
public static void Write(string message, LogMessageType messageType)
{
DoLog(message, messageType, null, Type.GetType("System.Object"));
}
/// <summary>
/// 写入日志
/// </summary>
/// <param name="message">日志信息</param>
/// <param name="messageType">日志类型</param>
/// <param name="type"></param>
public static void Write(string message, LogMessageType messageType, Type type)
{
DoLog(message, messageType, null, type);
}
/// <summary>
/// 断言
/// </summary>
/// <param name="condition">条件</param>
/// <param name="message">日志信息</param>
/// <param name="type">日志类型</param>
public static void Assert(bool condition, string message, Type type)
{
if (condition == false)
Write(message, LogMessageType.Info);
}
/// <summary>
/// 保存日志
/// </summary>
/// <param name="message">日志信息</param>
/// <param name="messageType">日志类型</param>
/// <param name="ex">异常</param>
/// <param name="type">日志类型</param>
private static void DoLog(string message, LogMessageType messageType, Exception ex,
Type type)
{
m_log = LogManager.GetLogger(type);
switch (messageType)
{
case LogMessageType.Debug:
AppLog.m_log.Debug(message, ex);
break;
case LogMessageType.Info:
AppLog.m_log.Info(message, ex);
break;
case LogMessageType.Warn:
AppLog.m_log.Warn(message, ex);
break;
case LogMessageType.Error:
AppLog.m_log.Error(message, ex);
break;
case LogMessageType.Fatal:
AppLog.m_log.Fatal(message, ex);
break;
}
}
/// <summary>
/// 日志类型
/// </summary>
public enum LogMessageType
{
/// <summary>
/// 调试
/// </summary>
Debug,
/// <summary>
/// 信息
/// </summary>
Info,
/// <summary>
/// 警告
/// </summary>
Warn,
/// <summary>
/// 错误
/// </summary>
Error,
/// <summary>
/// 致命错误
/// </summary>
Fatal
}
}
}
这个是我写入到数据库的代码,一直不能写入数据库
#2
第一,无法记录数据库很有可能是数据库连接问题,先确定连接字符串;
第二,log4net可以在任何地方调用写日志方法,跟错误来源没有关系;
第二,log4net可以在任何地方调用写日志方法,跟错误来源没有关系;
#3
但是必须抛出异常。他才能检测到记录下来是嘛?