您的位置:首页 > 其它

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: