Log4Net configuration learning

xiaoxiao2021-03-06  39

Logger hierarchy (level level)

Logger is an entity that has been named. Logger's name is case sensitive and follow the following rules:

1 If a logger's name is the prefix (pass "of the B logger name." Connection), then a logger is the grandfather level of B Logger.

2 If a logger's name and the N name of the B logger have no other logger name, a logger is the parent of the B logger.

Example: a logger name "foo.bar", the B logger name is "foo.bar.baz", the parent of A is B. Logger named "System" is a grandfather name name "System.Text.StringBuilder".

The root logger is located in the top level in the Logger level. It has three three rules:

1 Root Logger always exists.

2 Root Logger cannot pass name or taken.

3 Root Logger has a default Level value to debug.

Logger can get GetLogger with a statically method of log4net.logmanager class.

Level value: all / debug / info / warn / error / Fatal / Off

Level Inheritance Rules: If a logger does not define the Level, its Level value will inherit from its parent (defined for Level).

The log record request is implemented by calling the output method of the Logger instance. These output methods include Debug, Info, Warn, Error and Fatal.

By defining, the output method determines the Level of the log record request. For example, log is a logger instance, then the status log.info (".." log record request Level is Info.

If the LEVEL of the log record request is high than the LOGGER itself defines (or inherited), the request is feasible, otherwise it will not be feasible.

Simple rules: If the LEVEL of the log record request is L, the Logger itself defines (or inherited) Level is K, when L> = K, log record request is feasible.

Level Level: Debug

With the name, call the log4net.logmanager.getLogger method, you can get an instance reference to the same logger. This allows the reference to obtain the same Logger without passing the reference in the code. Basically, with biological parent-child relationship (father is always in the child), in log4net, Logger can be created and configured into any order. In particular, a parent logger may instantiate the child Logger, but it can be found in the child.

Appender

Log4net allows logs to be output in different ways, such as MS SQL, files, console or system logs, and more.

A logger can have multiple appenders. Each feasible log record request will be output to all appenders, which are referenced in the current logger, including parent loggers. In other words, Appender will add sexual inheritance in the hierarchical level of the logger. For example, a console appender is defined in root, then all Logges have at least one console appender. If a logger has a File appender, a logger and its sub-logger have two appenders. The Additive Property default is True. When set to false, the appender will be overwritten, and no longer inherit from the parent logger.

Filters is used to filter the Appender Acceptable log information.

Use the following Filter Appender only accepts the log record request between INFO to Fatal.

Use the appender using the following Filter only accepts log records that contain substrings "Database".

The first Filter will find "Database" in the log record information. If you find it no longer match the following Filter, Appner accepts and records the log. If you don't find the second filter, the filter will reject any log information.

Layouts is used to customize the output format.

Load configuration

Configfile Properties: When we define a log4net configuration file, you can specify the configuration file through this property.

ConfigfileExtension properties: This property can be used when the application is compiled into a different extension name. If the Sample program will be compiled into SAMPLE.exe, the configFileExtension is set to "config", then the configuration file name used is: sample.exe.config. Note that it cannot be used simultaneously with the configfile property.

Watch properties: Do not monitor the configuration file during the run. When the value is TRUE, FileSystemWatcher will use to monitor the content change, rename, and delete notifications of the configuration file.

Load the log4net configuration for the application, you can set the properties in the application set.

E.g:

[assmbly: log4net.config.domconfigurator (configfile = "sample.config")]]]]]

[assmbly: log4net.config.domconfigurator (configfileextension = "config")]]

You can also use any parameters, which will use the application of the application.

[Assembly: log4net.config.domconfigurator ()] Another way is to use the Domconfigurator class in the code

Log4net.config.domconfigurator.configure (

New fileInfo ("TestLogger.exe.config");

Use configureAndWatch (..) to specify a configuration file and monitor changes in the file.

PatternLayout

Each of the specified conversion symbols start with%, followed by an optional format symbol and a conversion symbol. The conversion symbol is used to specify the data type of the output, such as Level, Logger, Date, etc.

For example: conversion model "% -5p [% t]:% m% N"

Ilog log = logmanager.getlogger (TestApp);

LOG.DEBUG ("Message 1");

Log.Warn ("Message 2");

After conversion:

Debug [main]: message 1

Warn [main]: message 2

"% -5p" represents the LEVEL value of the output log record, with a width of 5 identities and left align.

Conversion symbol list:

a Output the name of AppDomain Creating a log C Default Output Logger's full name. The "{Digital}" can be followed by "{digital}", indicating the Logger name level (starting from the right) corresponding to the output. Such as "a.b.c", "% c {2}" output "B.C" C output call log record request. The "{Digital}" can be followed by "{digital}", indicating the output and the number corresponding to the number (including the namespace, starting from the right). d Output log recording time, follow "{time format}". The default is YYYY-MM-DD HH: MM: SS, FFF F Outputs the full name of the file calling the log record request. (Will affect speed) l Output some local information that calls log record requests. The number of lines such as class and member names, call files and calls declarations. (Extremely influence performance) l The number of declaration code lines that output call log record requests. (Extremely influential performance) m Output information to be output by the application. M Output member names of the call log record request. (Extremely influence performance) N outputs the Level value P outputting the log request of the Log Request

R Output The time (milliseconds) T output from the application starts to the log record request, and if the number of output threads is not named, the number U outputs the name of the current active user. (Principal.Identity.name) W Outputs the Windows ID of the current active user. x

X

% Output one%

转载请注明原文地址:https://www.9cbs.com/read-65215.html

New Post(0)