Log4net is the next excellent open source logging component of. Net. Log4net is very powerful for logging. It can divide logs into different levels, output them to different media in different formats.
Two days ago, a former colleague asked me how to integrate log4net into the core in the form of middleware. I answered without hesitation, someone should have done this kind of question, he said no. So, I went to the blog park to search. I found that it’s really not. It’s all traditional. Net. I directly used logmanager.getlogger. So I decided to investigate this problem when I had time on the weekend. The core of expanding the third-party logs is two interfaces: ilogger and iloggerprovider. So we built a solution and realized these two interfaces by brushing. When we checked the relevant data, we suddenly found that there were foreign colleagues who had already realized the sleeping trough. I had been working hard for a long time, and it completely collapsed…
Switch four ways
In the spirit of laziness and disdain for blind wheel / installation, we will directly switch the solution to open source implementation. In fact, by the way, the third party implements the source code, and the four channels are the same, realizing the two interfaces.
The solution is as follows:
Package references are as follows:
At the top, I don’t need to say much. The second one asks me. The third one is the protagonist of core integrated log4net. In particular, the latest version is 2.2.1, but I don’t refer to the latest version, but 2.1.0. At present, some third-party packages refer to the latest version of the atomic package, while the upper second ghost microsoft.aspnetcore.app framework package’s lower atomic package is not, so if the latest third-party package is referenced, it is likely to report an atom Incorrect package dependency version. There are two solutions: 1. Which package reports an error, and which atomic package’s latest version you install separately; 2. Switch the version of the third-party package to the next level, and find an implementation that is compatible with the current microsoft.aspnetcore.app. The problem of solution 1 is that you update the atomic package, its dependency package goes wrong again, you update its dependency, its dependency dependency goes wrong again… So, for convenience, I directly used the lower version of microsoft.extensions.logging.log4net.aspnetcore.
Log4net.config is no longer screenshot or code, just like the previous virtue.
Next, register log4net:
In the figure, the function of the first two lines is to filter out the logs below the warning level generated by components under the namespace beginning with system and Microsoft. In fact, they are mainly framework logs. The third line is to register log4net middleware.
Next, create a new controller and inject the effect of log practice:
In the two red boxes above, one is the constructor injection log interface, and the other is the call log component to record logs.
OK, let’s take a look at the logs generated by the system. First call:
OK, the log has been output successfully. Next, let’s look at the key output to the file:
See, the log has been successfully output to the file via log4net.
I don’t think it’s a good summary. The extension provided by. Net core is very open. Although log4net is a bit of a jerk at present, it shouldn’t be a problem for most of our. Net developers.
Well, the above is the whole content of this article. I hope that the content of this article has some reference learning value for your study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaer.