log4net for web快速入门
2009-05-18 18:46
155 查看
园子里翻了一些文章,大多语焉不详,或许我没有仔细了解。 现在终于调通了,加入了log4net自身的调试信息。可以查看log4net不输出日志的问题。 这是项目中实际的web.config文件。 建一个页面,贴入下面的代码就可以了,配置一下Global.asax就可以了。 自己撞了半天的墙,经验贴出来,希望能给大家帮助!自己也留个爪,备忘! 例子主要演示了: 1:把log文件存入到指定目录(很多例子是..\log\logfile,验证不对,应该是:..\\log\\logfile) 2:按日期生成Log文件 3:log4net自身的debug。 log4net为 1. 2. 0. 30714
实际中碰到个问题,用发布的项目不能写log,把本地的代码全部复制过去,就可以写了,不明白
Global.asax <%@ Application Language="C#" %> <script runat="server"> void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
log4net.Config.DOMConfigurator.Configure(); }
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown }
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs } void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started } void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised. }
</script> default.aspx using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
log4net.ILog logger = log4net.LogManager.GetLogger(this.GetType());
logger.Debug("this is test!");
}
} web.config <?xml version="1.0" encoding ="utf-8"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<!-- *************程序运行日志部分 ********************-->
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\\log\\logfile" />
<appendToFile value="true" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="2MB" />
<!--名称是否可以更改 为false为可以更改-->
<param name="StaticLogFileName" value="false" />
<!--文件名称-->
<param name="DatePattern" value="yyyyMMdd".txt"" />
<param name="RollingStyle" value="Composite" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%r] [%t] %-5p %c - %m%n------------------------------%n" />
</layout>
</appender>
<root>
<level value="debug" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
<!-- *************程序配置项设置部分 ********************-->
<appSettings>
<!--log4net self-->
<add key="log4net.Internal.Debug" value="true" />
</appSettings>
<system.web> <!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true"/>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace. <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Inetpub\wwwroot\log4.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
实际中碰到个问题,用发布的项目不能写log,把本地的代码全部复制过去,就可以写了,不明白
Global.asax <%@ Application Language="C#" %> <script runat="server"> void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
log4net.Config.DOMConfigurator.Configure(); }
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown }
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs } void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started } void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised. }
</script> default.aspx using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
log4net.ILog logger = log4net.LogManager.GetLogger(this.GetType());
logger.Debug("this is test!");
}
} web.config <?xml version="1.0" encoding ="utf-8"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<!-- *************程序运行日志部分 ********************-->
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\\log\\logfile" />
<appendToFile value="true" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="2MB" />
<!--名称是否可以更改 为false为可以更改-->
<param name="StaticLogFileName" value="false" />
<!--文件名称-->
<param name="DatePattern" value="yyyyMMdd".txt"" />
<param name="RollingStyle" value="Composite" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%r] [%t] %-5p %c - %m%n------------------------------%n" />
</layout>
</appender>
<root>
<level value="debug" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
<!-- *************程序配置项设置部分 ********************-->
<appSettings>
<!--log4net self-->
<add key="log4net.Internal.Debug" value="true" />
</appSettings>
<system.web> <!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true"/>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace. <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Inetpub\wwwroot\log4.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
相关文章推荐
- ArcGIS Web Mapping for Silverlight开发入门之一
- web前端之css快速入门
- Tiny快速入门之Web界面快速开发实践
- vue 快速入门与最佳实践(vue-cli, webpack, vue 最佳实践)
- R语言Web开发框架shiny包快速入门
- ComponentOne FlexGrid for WinForms 中文版快速入门(9)—过滤
- 使用ASP.NET 构建 Web 应用程序快速入门-8小时的免费培训视频 - Scott Hanselman的中文博客【转载】
- .Net Core WebAPI 基于Task的同步&异步编程快速入门
- Web Dynpro for java 开发入门
- ASP.NET MVC5快速入门--MyFirstWeb并发布到Windows Azure上
- Vue.js——60分钟webpack项目模板快速入门
- 7个知识点快速入门,Web前端初学者必备
- Android web开发快速入门
- Gradle2.0用户指南翻译——第十章. Web 应用程序快速入门
- Castle Active Record for .NET2.0快速入门示例
- ComponentOne FlexGrid for WinForms 中文版快速入门(1)--开始使用 FlexGrid
- ComponentOne FlexGrid for WinForms 中文版快速入门(4)--设置单元格格式
- 简单粗暴,详细得不要不要的 JavaWeb快速入门实例(1)
- 个人自制maven快速入门及其管理web项目多模块的依赖图文并茂教程
- Gradle For Android系列1:Gradle快速入门及AndroidStudio介绍