问题
I have created a Web App in Azure and a Web API in .Net core framework following the instructions in this link.
Now in my Web App , Application Insights is enabled.
In WebAPI has some similar code for logging.
public class Startup
{
public Startup()
{
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
var logger = loggerFactory.CreateLogger<ConsoleLogger>();
logger.LogInformation("Executing Configure()");
}
}
public class HomeController : Controller
{
ILogger _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("Executing Home/Index")
return View();
}
}
By default, it is printing some trace logs something like similar in Application Insights.
2019-01-02T07:22:49 Executing Home/Index
2019-01-02T07:22:49 Executing ObjectResult, writing value Microsoft.AspNetCore.Mvc.ControllerContext.
2019-01-02T07:22:50 Executed action [SomeActionName] (APIName) in 6487.7982ms
2019-01-02T07:22:50 Request finished in 6917.8019ms 200 application/json; charset=utf-8
Now my requirement is it should not print all the default logs in Application Insights. It has to print only those with _logger.LogInformation
. How and where do I disable this feature?
回答1:
You can use ITelemetryProcessor to filter out the unwanted messages(including the Trace).
1.You can test your project locally with app insights, and then use Application insights search to check the unwanted trace messages, check it's CategoryName(or others property which can specify it), like screenshot below:
2.Create a custom class which implements the ITelemetryProcessor. Here I use the CategoryName to filter out the unwanted trace messages, you can adjust the code yourself:
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace WebApplication1netcore4
{
public class MyTelemetryProcessor : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
public MyTelemetryProcessor(ITelemetryProcessor next)
{
this.Next = next;
}
public void Process(ITelemetry telemetry)
{
TraceTelemetry trace = telemetry as TraceTelemetry;
if (trace != null && trace.Context.Properties.Keys.Contains("CategoryName"))
{
//Here I just filter out 2 kinds of trace messages, you can adjust your code as per your need.
if (trace.Context.Properties["CategoryName"] == "Microsoft.AspNetCore.Hosting.Internal.WebHost" || trace.Context.Properties["CategoryName"] == "Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker")
{
//return means abandon this trace message which has the specified CategoryName
return;
}
}
if (trace == null)
{
this.Next.Process(telemetry);
}
if (trace != null)
{
this.Next.Process(trace);
}
}
}
}
3.In Startup.cs -> ConfigureServices() method, add the following code:
services.AddApplicationInsightsTelemetry();
services.AddApplicationInsightsTelemetryProcessor<MyTelemetryProcessor>();
4.After test, you can see the unwanted trace messages are filtered out.
回答2:
Not sure how you have enabled Ilogger integration with application insights, but the currently supported way is described here. https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Logging
The second parameter to the extension method controls which messages gets picked up by application insights. loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);
This should limit logs Warning or above to be sent to application insights. You can ofcourse use a ITelemetryprocessor to filter the logs, but thats more expensive as logs are already collected but later dropped, wasting computation cycles/memory.
回答3:
Another way to disable certain types of log messages is through the appsettings.json:
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft.AspNetCore.Hosting.Internal.WebHost": "None",
"Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker": "None",
}
},
This disables the same telemetry as @Ivan Young's answer but allows you to change the settings without code changes.
来源:https://stackoverflow.com/questions/54002813/disable-default-trace-log-messages-from-web-app-in-application-insights