英文:
Exclude specific controller from logging in asp.net core
问题
有一个在我们的ASP.net核心应用程序中的HealthCheck控制器,只返回200 - OK的GET资源。 这个资源每5秒被调用一次,会导致我们的日志文件被大量记录。我们正在使用Log4net。
如何排除特定的控制器/资源不记录日志?
编辑:日志条目来自Microsoft.AspNetCore.*命名空间,它们是有关请求启动/完成/路由匹配等信息的。我想只为单个控制器关闭这些日志记录。
英文:
There is a HealthCheck controller with GET resource in our ASP.net core app that simply returns 200 - OK. This resource is called every 5 seconds which spams our log files. We are using Log4net.
How can I exclude specific controller/resource from logging?
EDIT: The log entries come from Microsoft.AspNetCore.* namespace, they are the informations about request starting/finishing/route matching/etc. I'd like to turn these off for single controller only.
答案1
得分: 2
你指定了你正在使用Log4Net,但我假设你实际上是在使用Microsoft.Extensions.Logging
的Log4Net 提供程序。如果确实如此,你可以通过配置来过滤日志。大致如下:
{
"Logging": {
...
"LogLevel": {
"Namespace.To.MyController": "Error",
"Default": "Debug"
}
}
}
这将仅在该命名空间中记录,如果消息级别为Error或更高,则会过滤掉信息级别的日志,这可能是你所说的“垃圾邮件”所指的。
英文:
You specify that you're using Log4Net, but I'm assuming you're actually using the Log4Net provider with Microsoft.Extensions.Logging
. If that is in fact the case, you can filter the logs via config. Something along the lines of:
{
"Logging": {
...
"LogLevel": {
"Namespace.To.MyController": "Error",
"Default": "Debug"
}
}
}
That will then only log in that namespace, if the level of the message is Error or higher, filtering out things like Information level logs, which is what you're likely referring to as the "spam".
答案2
得分: 1
我找到了一种使用 WebHostBuilder.ConfigureLogging 的方法。
.ConfigureLogging(x => x.AddFilter("Microsoft.AspNetCore", Microsoft.Extensions.Logging.LogLevel.Warning))
可悲的是,你必须硬编码这个,似乎没有办法从配置中完成这个操作。
英文:
I've found a way using WebHostBuilder.ConfigureLogging.
.ConfigureLogging(x => x.AddFilter("Microsoft.AspNetCore", Microsoft.Extensions.Logging.LogLevel.Warning))
Sadly you have to hard-code this, there seems to be no way to do this from the config.
答案3
得分: 0
希望这有所帮助
一个过滤器确实有效,但我更愿意直接关闭记录器(或记录器层次结构),如下所示:
<logger name="YourNameSpace.WithNoLogging" additivity="false">
<level value="OFF" />
</logger>
<logger name="MyClass" additivity="false">
<level value="OFF" />
</logger>
<root>
<level value="ALL" />
<appender-ref ref="YourAppender" />
</root>
第二个 "example" 关闭了您类的记录(根据您的问题)。
英文:
I hope this help
A filter certainly works but I would prefer to turn off the logger (or logger hierarchy) directly like this:
<logger name="YourNameSpace.WithNoLogging" additivity="false">
<level value="OFF" />
</logger>
<logger name="MyClass" additivity="false">
<level value="OFF" />
</logger>
<root>
<level value="ALL" />
<appender-ref ref="YourAppender" />
</root>
The second "example" turns off logging for your class (according to your question).
答案4
得分: 0
以下是翻译好的内容:
有一种方法可以使用nlog来实现,但也可能使用log4net。这需要您自己编写自定义代码。
创建一个中间件,将活动ID分配给Trace.CorrelationManager.ActivityId。还要在HttpContext项目中记录活动ID,如果请求路径来自您的健康监视器。
在您的日志配置中,您将需要一个类似于NLog.Web.AspNetCore的包,以获取两个活动ID的访问权限,以便使用日志筛选器:
在下面的代码中,我的上下文项目是"ActivityIdToSink"
<logger name="Microsoft.AspNetCore.HttpLogging.*" minLevel="Trace" final="true" writeTo="allFile">
<filters defaultAction="Neutral">
<when condition="equals('${aspnet-item:variable=ActivityIdToSink}', '${activityId}')" action="Ignore" />
</filters>
</logger>
这将导致健康探针不会被写入到allFile日志中。
英文:
There is a way to do it with nlog but may be possible with log4net. It does require custom code on your part.
Create a middleware that assigns an activity id to the Trace.CorrelationManager.ActivityId. Also, record the activity id in a HttpContext item if the request path is from your health monitor.
In your logging config, you will need a package similar to NLog.Web.AspNetCore to get access to both activity ids so as to use a logging filter:
In the code below, my context item is "ActivityIdToSink"
<logger name="Microsoft.AspNetCore.HttpLogging.*" minLevel="Trace" final="true" writeTo="allFile">
<filters defaultAction="Neutral">
<when condition="equals('${aspnet-item:variable=ActivityIdToSink}', '${activityId}')" action="Ignore" />
</filters>
</logger>
This will cause the health probe to not be written to the allFile log.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论