2020年9月14日 星期一

[研究] [ASP.NET] Apache log4net 2.0.10 安裝與試用 (NuGet)

[研究] [ASP.NET] Apache log4net 2.0.10 安裝與試用 (NuGet)

2020-09-14
Apache log4net 官方網站
http://logging.apache.org/log4net/

環境:Visual Studio 2019

官方範例
http://logging.apache.org/log4net/download_log4net.cgi
http://ftp.twaren.net/Unix/Web/apache//logging/log4net/source/
下載 log4net-2.0.10-src.zip




Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
Inherits="log4netTest.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
</body>
</html>


Default.aspx.cs
using log4net;
using System;

namespace log4netTest
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            // Define a static logger variable so that it references the
            // Logger instance named "MyApp".
            //private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));
            //private static readonly ILog log = LogManager.GetLogger(typeof(Default));
            // typeof 中是 class 名稱;
            ILog log = LogManager.GetLogger(typeof(Default));

            // Set up a simple configuration that logs on the console.
            // https://logging.apache.org/log4net/release/manual/configuration.html
            log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/log4net.config")));
            //log4net.Config.XmlConfigurator.Configure(new FileInfo(@"\config.log4net"));

            log.Info("Entering application.");
            log.Info("Something.");
            log.Info("Exiting application.");

            // This will shutdown the log4net system
            log4net.LogManager.Shutdown();
        }
    }
}

參考
https://logging.apache.org/log4net/release/manual/configuration.html
log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
    <appender name="Console" type="log4net.Appender.ConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
            <!-- Pattern to output the caller's file name and line number -->
            <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
        </layout>
    </appender>
    
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
        <file value="example.log" />
        <appendToFile value="true" />
        <maximumFileSize value="100KB" />
        <maxSizeRollBackups value="2" />
 
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level %thread %logger - %message%newline" />
        </layout>
    </appender>
    
    <root>
        <level value="DEBUG" />
        <appender-ref ref="Console" />
        <appender-ref ref="RollingFile" />
    </root>
</log4net>
</configuration>

檢視
C:\CodeTemp\log4netTest2\log4netTest\example.log 檔案
內容
INFO 6 log4netTest.Default - Entering application.
INFO 6 log4netTest.Default - Something.
INFO 6 log4netTest.Default - Exiting application.

(完)

沒有留言:

張貼留言