Example 1 - log4net simplest sort by date save the log Console program

Disclaimer: This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise the right to pursue legal responsibilities. https://blog.csdn.net/mymhj/article/details/7520095

aims:

      By using Log4net framework, created by date output log sample program in a text file.

demand:

      1 using log4net framework provides logging, system log management.

      Each day with log 2 log text corresponding to a store. (Log text name to distinguish by date)

demand analysis:

       Maintenance log4net.dll component library 1  

       2 The use of the log file to roll back way, so Appender selection: RollingFileAppender.

 Rollback form: date.

         Initially set the date format: yyyyMMdd-HHmmm

Detailed Description:

     (1) create a console application c #

      (2) add a reference log4net.dll

      (3) add a text file in the project, the file extension to: log.set, Note: To set this property file is: if there is a newer copy

  The contents of the file are:

<?xml version="1.0" encoding="utf-8" ?>
<log4net xmlns="urn:log4net">
  <root xmlns="">
    <level value="ALL" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
  <appender xmlns="" name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="log\\Log_"/>
    <param name="AppendToFile" value="true"/>
    <param name="StaticLogFileName" value="false"/>
    <param name="DatePattern" value="yyyyMMdd&quot;.log&quot;"/>
    <param name="RollingStyle" value="Date"/>
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>    <!--关于这里参数设置-->
    </layout>
  </appender>
</log4net>

(4) in the project file AssembleInfo.cs added: [assembly: log4net.Config.XmlConfigurator (ConfigFile = "log.set", Watch = true)]

        (5) encoding

               . A file in your Program added: using: log4net

               b. Copy this code to your Main function

            = Log log4net.LogManager.GetLogger the ILog (typeof (Program)); 
            the Random Random new new = the Random ();
            for (int I = 0; I <2; I ++)
            {
                // log errors 
                IF (log.IsErrorEnabled)
                    log. Debug ( "you cause an error, error ID is:." + Random.Next () ToString ());


                // records critical errors  
                IF (log.IsFatalEnabled)
                    log.Fatal ( "you who led a summary error, may cause the system to terminate, ID is:. "+ Random.Next () ToString ()); 
                // record general information 
                IF (log.IsInfoEnabled)
                    log.info (" you plan to record a message, id is: "+ random. the Next () ToString ());.  
                // record debugging information  
                IF (log.IsDebugEnabled)
                    log.debug ( "debug information, the debugger ID is:." + Random.Next () the ToString ());
                // record the warning information 
                IF (log.IsWarnEnabled)
                {
                    log.Warn ( "Warning: warning ID is: "+ Random.Next () the ToString ());.
                }
            }

        (6) test once every other day to run your console application. Suppose I once ran yesterday and today to run again. Then there will be a log folder under bin \ debug directory of the program, the folder will appear in two text files "Log_20120427.log" and "Log_20120428". Open a file which you can see logging.

 I would like to upload the sample program, rogue csdn too so bad. Behind the download link will provide an example

         (7) The following are several common rollback journal storage,

         (collect)

RollingFileAppender mainly based FileAppender built, many properties are inherited from FileAppender method, so the option is set, there are many identical.

The following example of the log file named log.txt, the log file name is the file size transform, and if the old log file size to 100KB, it will transform a file named log.txt.1, log.txt.2, log.txt.3 ..., has up to 10

<appender name = "RollingFileAppender" of the type = "log4net.Appender.RollingFileAppender">   
        <-! log file name -> 
    <File value = "log.txt" /> 
    <-! Are appended to the file -> 
    <appendToFile = value "to true" /> 
    <-! converts the log file according to the file size -> 
    <rollingStyle value = "size" /> 
    <-! maximum transformation number -> 
    <maxSizeRollBackups value = "10" /> 
    <! - maximum file size -> 
    <maximumFileSize value = "100KB" /> 
    <! - whether static log file name -> 
    <staticLogFileName value = "to true" /> 
    <layout of the type = "log4net. Layout.PatternLayout "> 
        < conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/> 
    </layout> 
</appender>
The following example a time period converted log file name, change log per minute are file name, file name log logfileyyyyMMdd-HHmm, supplement described herein, if it later to add the suffix .log file name must be escaped characters, such as <datePattern value = "yyyyMMdd-HHmm & quot; .log & quot;" />

<appender name = "RollingLogFileAppender" of the type = "log4net.Appender.RollingFileAppender"> 
        <-! log file names that begin -> 
    <File value = "logfile" /> 
    <-! whether to append to the file -> 
    <appendToFile = value "to true" /> 
    <-! format converting a date -> 
    <rollingStyle value = "a date" /> 
    <-! date format -> 
    <the datePattern value = "yyyyMMdd-HHMM" /> 
    < type = layout "log4net.Layout.PatternLayout"> 
        <ConversionPattern value = "% DATE [Thread%]%% -5level Logger [% {Property the NDC}] - Message% NEWLINE%" /> 
    </ layout> 
</ the appender>
the following examples use the integrated use of date and file size conversion log file name.

<appender name = "RollingLogFileAppender" of the type = "log4net.Appender.RollingFileAppender"> 
    <-! log file names that begin -> 
    <File value = "logfile" /> 
    <-! whether to append to the file -> 
    <appendToFile = value "to true" /> 
    <-! mixture date and size of the log file name conversion -> 
    <rollingStyle value = "Composite" /> 
    <-! date format -> 
    <the datePattern value = "yyyyMMdd" /> 
    <! - the maximum number of transform -> 
    <maxSizeRollBackups value = "10" /> 
    <! - the maximum file size -> 
    <maximumFileSize value = "1MB" /> 
    <layout type = "log4net.Layout.PatternLayout "> 
        < conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> 
    </layout> 
</appender>
Here is an example every time the program is running, the original log files are backed up, when the log file to run is greater than 50G will transform the log file name.

<appender name = "RollingLogFileAppender" of the type = "log4net.Appender.RollingFileAppender"> 
    <-! log file name -> 
    <File value = "logfile.txt" /> 
    <-! not appended to the original file -> 
    <appendToFile = value "to false" /> 
    <-! transformation file size of log file name -> 
    <rollingStyle value = "size" /> 
    <-! maximum number of conversion is not limited -1 -> 
    <maxSizeRollBackups = value "-. 1" /> 
    <-! maximum file size -> 
    <maximumFileSize value = "50GB" /> 
    <layout type = "log4net.Layout.PatternLayout"> 
        <ConversionPattern value = "% DATE [Thread%] % -5level% logger [% property {  NDC}] -% message% newline "/>
    </ layout> 
</ appender>
The following is a partial Public Instance Properties RollingFileAppender class (public property), without all the tests, it is inevitable fallacy, if you know, please do not hesitate to write report.
CountDirection: definition of the current file is the smallest number of files or maximum number of files. The default is -1, always a smaller number.
datePattern: DateFormatter date format, string type.
Encoding: is the encoded file, the default is Default, that is, the program code itself.
FilterHead: delimiter header.
ImmediateFlush: immediately indicate whether the output to a file, Boolean.
LockingModel: file locking type, RollingFileAppender itself is not thread-safe, thread-safe if no restrictions in the program can be configured here, to ensure the safety of written. There are two types: FileAppender.ExclusiveLock and FileAppender.MinimalLock
the Name: Set the name of the present Appander.
SecurityContext: less applications, encrypting only the log type, using SecurityContextProvider conversion. (When the log is relatively high confidentiality requirements should be applied on it, quite comprehensive Log4 considered)
Threshold: Set the level of content, such as: DEBUG, INFO, and so on.

RollingFileAppender mainly based FileAppender built, many properties are inherited from FileAppender method, so the option is set, there are many identical.

The following example of the log file named log.txt, the log file name is the file size transform, and if the old log file size to 100KB, it will transform a file named log.txt.1, log.txt.2, log.txt.3 ..., has up to 10

<appender name = "RollingFileAppender" of the type = "log4net.Appender.RollingFileAppender">  
        <-! log file name ->
    <File value = "log.txt" />
    <-! Are appended to the file ->
    <appendToFile = value "to true" />
    <-! converts the log file according to the file size ->
    <rollingStyle value = "size" />
    <-! maximum transformation number ->
    <maxSizeRollBackups value = "10" />
    <! - maximum file size ->
    <maximumFileSize value = "100KB" />
    <! - whether static log file name ->
    <staticLogFileName value = "to true" />
    <layout of the type = "log4net. Layout.PatternLayout ">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
    </layout>
</appender>
The following example a time period converted log file name, change log per minute are file name, file name log logfileyyyyMMdd-HHmm, supplement described herein, if it later to add the suffix .log file name must be escaped characters, such as <datePattern value = "yyyyMMdd-HHmm & quot; .log & quot;" />

<appender name = "RollingLogFileAppender" of the type = "log4net.Appender.RollingFileAppender">
        <-! log file names that begin ->
    <File value = "logfile" />
    <-! whether to append to the file ->
    <appendToFile = value "to true" />
    <-! format converting a date ->
    <rollingStyle value = "a date" />
    <-! date format ->
    <the datePattern value = "yyyyMMdd-HHMM" />
    < type = layout "log4net.Layout.PatternLayout">
        <ConversionPattern value = "% DATE [Thread%]%% -5level Logger [% {Property the NDC}] - Message% NEWLINE%" />
    </ layout>
</ the appender>
the following examples use the integrated use of date and file size conversion log file name.

<appender name = "RollingLogFileAppender" of the type = "log4net.Appender.RollingFileAppender">
    <-! log file names that begin ->
    <File value = "logfile" />
    <-! whether to append to the file ->
    <appendToFile = value "to true" />
    <-! mixture date and size of the log file name conversion ->
    <rollingStyle value = "Composite" />
    <-! date format ->
    <the datePattern value = "yyyyMMdd" />
    <! - the maximum number of transform ->
    <maxSizeRollBackups value = "10" />
    <! - the maximum file size ->
    <maximumFileSize value = "1MB" />
    <layout type = "log4net.Layout.PatternLayout ">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>
Here is an example every time the program is running, the original log files are backed up, when the log file to run is greater than 50G will transform the log file name.

<appender name = "RollingLogFileAppender" of the type = "log4net.Appender.RollingFileAppender">
    <-! log file name ->
    <File value = "logfile.txt" />
    <-! not appended to the original file ->
    <appendToFile = value "to false" />
    <-! transformation file size of log file name ->
    <rollingStyle value = "size" />
    <-! maximum number of conversion is not limited -1 ->
    <maxSizeRollBackups = value "-. 1" />
    <-! maximum file size ->
    <maximumFileSize value = "50GB" />
    <layout type = "log4net.Layout.PatternLayout">
        <ConversionPattern value = "% DATE [Thread%] % -5level% logger [% property { NDC}] -% message% newline "/>
    </ layout>
</ appender>
The following is a partial Public Instance Properties RollingFileAppender class (public property), without all the tests, it is inevitable fallacy, if you know, please do not hesitate to write report.
CountDirection: definition of the current file is the smallest number of files or maximum number of files. The default is -1, always a smaller number.
datePattern: DateFormatter date format, string type.
Encoding: is the encoded file, the default is Default, that is, the program code itself.
FilterHead: delimiter header.
ImmediateFlush: immediately indicate whether the output to a file, Boolean.
LockingModel: file locking type, RollingFileAppender itself is not thread-safe, thread-safe if no restrictions in the program can be configured here, to ensure the safety of written. There are two types: FileAppender.ExclusiveLock and FileAppender.MinimalLock
the Name: Set the name of the present Appander.
SecurityContext: less applications, encrypting only the log type, using SecurityContextProvider conversion. (When the log is relatively high confidentiality requirements should be applied on it, quite comprehensive Log4 considered)
Threshold: Set the level of content, such as: DEBUG, INFO, and so on.


This sample program wanted to upload, rogue csdn too well spent.

Guess you like

Origin blog.csdn.net/mymhj/article/details/7520095