Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How .net core integrates log4net

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to integrate log4net with. net core. The introduction in this article is very detailed and has certain reference value. Interested friends must read it!

1. background

Two days ago, a former colleague asked me how to integrate log4net into core in the form of middleware. I answered without thinking, this kind of question should have been done by someone, he said no. So, I went to the blog garden search, found really no, all are traditional. NET that set, direct LogManager.GetLogger, so take advantage of the weekend time, decided to investigate this problem. A quick look at the website shows that the core of extending third-party logs lies in two interfaces: ILogger and ILoggerProvider. So build a solution, brush brush to achieve these two interfaces, to achieve almost check the relevant information, suddenly found, f * ck, there have been foreign colleagues to achieve, I f * cking hard to write for a long time, this completely collapsed.

2. switch four way

In the spirit of laziness, contempt for blind wheel/pretentious mentality, directly switch the solution to open source implementation. In fact, by the way, look at the third-party source code, the four ways are also consistent, to achieve those two interfaces.

The solution is as follows:

Package references are as follows:

Don't ask me the second one, the third one is the protagonist of core integration log4net. In particular, the latest version is 2.2.1, but I did not refer to the latest version, but 2.1.0, because some of the latest versions of third-party packages refer to the latest atomic package, and the second ghost above Microsoft.AspNetCore.App The atomic package at the bottom of this framework package is not, so if you refer to the latest third-party package, it is likely to report an atomic package dependency version error. There are two solutions: 1, which package reports errors, which atomic package you install separately;2, switch the version of the third-party package to a lower level, and find an implementation that is compatible with the current Microsoft.AspNetCore. App. The problem with solution 1 is that you update the atomic package, its dependencies go wrong again, you update its dependencies, its dependencies go wrong again. So, for convenience, I went straight to an earlier version of Microsoft.Extensions.Logging.Log4Net.AspNetCore.

Log4net.config no longer screenshots or code, just like before.

Next, register log4net:

In the figure, the first two lines are used to filter out the warning level logs generated by components under the namespace at the beginning of 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 log into practice.

The two red boxes in the figure above, the top one is the constructor injection log interface, and the bottom is the call log component logging.

Okay, next we'll look at the logs generated by the system, calling:

Console window:

OK, the log has been exported successfully, next, we look at the critical output to the file:

No, the log has been successfully exported to a file via log4 net.

The above is all the content of ". net core how to integrate log4net" this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report