csharp log4net的

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp log4net的相关的知识,希望对你有一定的参考价值。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  ...
  <log4net>
    <appender name="Basic" type="log4net.Appender.RollingFileAppender">
      <file value="C:/Log_Booking/Franky.log" />
      <appendToFile value="true" />
      <maximumFileSize value="100KB" />
      <maxSizeRollBackups value="2" />z
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger{3} %M : %line - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="All" />
      <appender-ref ref="Basic" />
    </root>
  </log4net>
  <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
</configuration>
using System.Reflection;
using System.Runtime.CompilerServices;

//
// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
//
[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]		

//
// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers 
// by using the '*' as shown below:

[assembly: AssemblyVersion("1.45.0.1")]

//
// In order to sign your assembly you must specify a key to use. Refer to the 
// Microsoft .NET Framework documentation for more information on assembly signing.
//
// Use the attributes below to control which key is used for signing. 
//
// Notes: 
//   (*) If no key is specified, the assembly is not signed.
//   (*) KeyName refers to a key that has been installed in the Crypto Service
//       Provider (CSP) on your machine. KeyFile refers to a file which contains
//       a key.
//   (*) If the KeyFile and the KeyName values are both specified, the 
//       following processing occurs:
//       (1) If the KeyName can be found in the CSP, that key is used.
//       (2) If the KeyName does not exist and the KeyFile does exist, the key 
//           in the KeyFile is installed into the CSP and used.
//   (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility.
//        When specifying the KeyFile, the location of the KeyFile should be
//        relative to the "project output directory". The location of the project output
//        directory is dependent on whether you are working with a local or web project.
//        For local projects, the project output directory is defined as
//       <Project Directory>\obj\<Configuration>. For example, if your KeyFile is
//       located in the project directory, you would specify the AssemblyKeyFile 
//       attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
//        For web projects, the project output directory is defined as
//       %HOMEPATH%\VSWebCache\<Machine Name>\<Project Directory>\obj\<Configuration>.
//   (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
//       documentation for more information on this.
//
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile(@"c:\Barcelo\Deploy\Barcelo.snk")]
[assembly: AssemblyKeyName("")]
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
[assembly: AssemblyFileVersion("1.45.0.1")]

%date [%thread] %-5level %logger{3} %M : %line - %message
using log4net;
using log4net.Config;

...

private static readonly ILog log = LogManager.GetLogger(typeof(classname));

...

BasicConfigurator.Configure();

...

log.Info("Test");
https://support.stackify.com/hc/en-us/articles/208397953

.NET 4.0 app pools:

In both [system drive]:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config and [system drive]:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config

Find the xml path of:

 <system.web><httpModules>
And add the following:

<add name="StackifyHttpModule_Net40" type="StackifyHttpTracer.StackifyHttpModule,StackifyHttpTracer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=93c44ce23f2048dd"/>
The file should look like the example below:

  <system.web>
	<httpModules>
      <!-- existing content -->
      <add name="StackifyHttpModule_Net40" type="StackifyHttpTracer.StackifyHttpModule,StackifyHttpTracer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=93c44ce23f2048dd"/>
    </httpModules>
  </system.web>
Once the configuration changes have been made, save the change and recycle the app pool.
<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="BaseInfo" type="log4net.Appender.RollingFileAppender">
    <file value="log_file.log" />
    <appendToFile value="true" />
    <maximumFileSize value="100KB" />
    <maxSizeRollBackups value="2" />

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger{3} %M : %line - %message%newline" />
    </layout>
  
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="INFO" />
      <levelMax value="INFO" />
    </filter>
  </appender>

  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="C:/Log_Booking/TrazasDispo.log" />
    <appendToFile value="true" />
    <maximumFileSize value="10MB" />
    <maxSizeRollBackups value="2" />

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger{3} %M : %line - %message%newline" />
    </layout>
  </appender>

  <appender name="RollingFileDAL" type="log4net.Appender.RollingFileAppender">
    <file value="C:/Log_Booking/CallCenterDAL.log" />
    <appendToFile value="true" />
    <maximumFileSize value="10MB" />
    <maxSizeRollBackups value="2" />

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger{3} %M : %line - %message%newline" />
    </layout>
  </appender>

  <appender name="RollingFileDisponibilidad" type="log4net.Appender.RollingFileAppender">
    <file value="C:/Log_Booking/Disponibilidad.log" />
    <appendToFile value="true" />
    <maximumFileSize value="10MB" />
    <maxSizeRollBackups value="2" />

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger{3} %M : %line - %message%newline" />
    </layout>
  </appender>

  <appender name="RollingFileGlobalCollect" type="log4net.Appender.RollingFileAppender">
    <file value="C:/Reservation_Payment_Logs/Envio_Transacciones_Global_Collect.log" />
    <appendToFile value="true" />
    <maximumFileSize value="10MB" />
    <maxSizeRollBackups value="2" />

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger{3} %M : %line - %message%newline" />
    </layout>
  </appender>


  <logger name="Barcelo.Portal.Pasarelas">
    <level value="INFO" />
    <appender-ref ref="RollingFileGlobalCollect" />
  </logger>
  
  
  <logger name="Disponibilidad">
    <level value="DEBUG" />
    <appender-ref ref="RollingFileDisponibilidad" />
  </logger>
  
  <logger name="Barcelo.Portal.Reservas.DAL">
    <level value="DEBUG" />
    <appender-ref ref="RollingFileDAL" />
  </logger>

  <root>
    <level value="INFO" />
    <appender-ref ref="Console" />
    <appender-ref ref="RollingFile" />
  </root>

  <root>
    <level value="INFO" />
    <appender-ref ref="Console" />
    <appender-ref ref="BaseInfo" />
  </root>
  
</log4net>

以上是关于csharp log4net的的主要内容,如果未能解决你的问题,请参考以下文章

csharp log4net的

csharp log4net记录器扩展

csharp log4net的配置のファイル

csharp 使用Log4net日志记录的自定义Web API过滤器。

csharp 用C#编写的简单SMTP邮件客户端助手类,用于异步发送电子邮件。注意:使用Log4Net进行日志记录。

csharp log4net的を各クラスで毎回GetLoggerするか,上下文内の记录器をGetLoggerして使いまわすかでコストの差を见てみた。