The previous statement said: Repository can be called a log4net container created based on a log4net configuration section. It creates all other objects (Logger/Appender/Filter/Layout) according to the instructions in the log4net configuration section) and keep their instances for your use at any time.
Each Repository has its own unique name, such as root.
Generally, an AppDomain (or process) has a Repository, which can be used by all Assembly in this AppDomain. Repository needs to implement ILoggerRepository. In log4net, log4net. Repository. Hierarchy. Hierarchy implements ILoggerRepository by inheriting the metadata, which is also the only class that implements ILoggerRepository in log4net.
Hierarchy
What is Hierarchy?
Hierarchy stores all Logger created through the configuration file. Because Logger has a parent-child relationship, Hierarchy stores all Logger by using the inheritance tree. The Root node is the familiar Root node, for example:
LoggerName |
Log Level |
From parentLoggerInheritance level |
Root |
INFO |
INFO |
My |
None |
INFO |
My.net |
DEBUG |
DEBUG |
My.net. tcp |
None |
DEBUG |
The corresponding configuration file should be:
<Root>
<Level value = "INFO"/>
<Appender-ref = "leleappender"/>
</Root>
<Logger name = "my">
<Appender-ref = "leleappender"/>
</Logger>
<Logger name = "my.net">
<Appender-ref = "leleappender"/>
</Logger>
<Logger name = "my.net. tcp">
<Filter type = "log4net. Filter. LevelRangeFilter">
<Param name = "LevelMin" value = "DEBUG"/>
<Param name = "LevelMax" value = "INFO"/>
</Filter>
<Appender-ref = "ColoredConsoleAppender"/>
</Logger>
In the preceding example, three Logger types are defined and stored in Hierarchy. The three Logger form an inheritance relationship,ChildLoggerAll attributes not defined inMedium inheritance.
Once your application obtains the ILog (that is, the logger agent) through log4net. LogManager. GetLogger (), it will find the corresponding Logger from the inheritance tree of Hierarchy.
Log4net. LogManager. GetLogger () to get the root
Log4net. LogManager. GetLogger ("my") to get my logger
In this way, you can set the corresponding log4net configuration for different namespaces or even a class in the dataset. In the preceding example, "my.net. tcp" can implement different log behavior from its parent Logger.
Use differentRepository
If you need to use different <log4net>... </Log4net> in the configuration section, or use different log4net configuration files, you can use different Repository.
For example, in the my.net. tcp assembly, add the statement [assembly: log4net. Config. DOMConfigurator (ConfigFile = "my.net. tcp. config", Watch = true)]
In this way, you can use a configuration file to create a new Repository.
You can also name your Repository: [assembly: log4net. Config. AliasRepository ("myrepository")]
How to shareRepository
Without all the changes mentioned above, all the assembly in one AppDomain share the default Repository, but some work needs to be done to share another Repository. The following requirements are generated:
1. The two applications share a log4net configuration to process logs in the same way.
2. The two AppDomains need to share a log4net configuration to process logs in the same way. This requirement is especially critical when you dynamically upgrade the Assembly during runtime.
First, the log4net assembly must be named Repository: [assembly: log4net. Config. AliasRepository ("myrepository")]
For subsequent assembly, you only need to reference it: [assembly: log4net. Config. Repository ("myrepository")]
In this way, when two AppDomains write the same log file, a file sharing conflict error may occur (the file has been locked and cannot be written). You need to modify the configuration, add the lockingModel configuration to RollingLogFileAppender, for example:
<Appender name = "RollingLogFileAppender" type = "log4net. Appender. RollingFileAppender">
<Param name = "File" value = "log \ TaskScheduleServer. log"/>
<LockingModel type = "log4net. Appender. FileAppender + MinimalLock"/>
</Appender>