Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off the logging done by the ASP.NET core framework

How do I turn off the logging done by ASP.NET for each request e.g.

INFO 09:38:41 User profile is available. Using 'C:\Users\xxxx xxxx\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
DEBUG 09:38:41 Hosting starting
DEBUG 09:38:41 Hosting started
INFO 09:38:41 Request starting HTTP/1.1 GET http://localhost:23369/
INFO 09:38:41 Request starting HTTP/1.1 DEBUG http://localhost:23369/ text/html DEBUG 09:38:41 DEBUG requests are not supported
DEBUG 09:38:41 The request path / does not match a supported file type
DEBUG 09:38:41 Request successfully matched the route with name 'default' and template '{controller=Home}/{action=Index}/{id?}'. DEBUG 09:38:41 Request successfully matched the route with name 'default' and template '{controller=Home}/{action=Index}/{id?}'. DEBUG 09:38:41 Executing action Forums.Controllers.HomeController.Index
DEBUG 09:38:41 Executing action Forums.Controllers.HomeController.Index
INFO 09:38:41 Executing action method Forums.Controllers.HomeController.Index with arguments () - ModelState is Valid'
INFO 09:38:41 Executing action method Forums.Controllers.HomeController.Index
..

I couldn't find yet how I can turn this logging off...

This is my Configure method in the Startup class:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {     loggerFactory.AddProvider(new Log4NetProvider());      if (env.IsDevelopment())     {         app.UseBrowserLink();         app.UseDeveloperExceptionPage();         app.UseDatabaseErrorPage();     }     else     {         app.UseExceptionHandler("/Home/Error");          // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859         try         {             using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()                 .CreateScope())             {                 serviceScope.ServiceProvider.GetService<ApplicationDbContext>()                      .Database.Migrate();             }         }         catch { }     }      app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());      app.UseStaticFiles();      app.UseIdentity();      // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715      app.UseMvc(routes =>     {         routes.MapRoute(             name: "default",             template: "{controller=Home}/{action=Index}/{id?}");     }); } 

And this is my project.json file:

"dependencies": {   "EntityFramework.Commands": "7.0.0-rc1-final",   "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",   "log4net": "2.0.5",   "Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final",   "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final",   "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",   "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",   "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",   "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",   "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",   "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",   "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",   "Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",   "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",   "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",   "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",   "Microsoft.Extensions.Logging": "1.0.0-rc1-final",   "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final" },  "commands": {   "web": "Microsoft.AspNet.Server.Kestrel",   "ef": "EntityFramework.Commands" },  "frameworks": {   "dnx451": { } }, 

Update:
My log4net provider was taken from here

like image 985
gdoron is supporting Monica Avatar asked Feb 07 '16 07:02

gdoron is supporting Monica


People also ask

What is logging framework in ASP.NET Core?

ASP.NetServer Side ProgrammingProgramming. Logging is the process of recording events in software as they happen in real-time, along with other information such as the infrastructure details, time taken to execute, etc. Logging is an essential part of any software application.

How do I enable login in NET Core?

To enable the service of logging, we need to add the provider extension method in ConfigureLogging method of instance of WebHostBuilder class under main function. A logging provider takes a message which we create with ILogger object and display or store them.

Which logging framework is best for .NET Core?

NLog is one of the most popular, and one of the best-performing logging frameworks for . NET. Setting up NLog is fairly simple. Developers can use Nuget to download the dependency, then edit the NLog.

What is the use of ILogger in .NET Core?

ILoggerFactory is a factory interface that we can use to create instances of the ILogger type and register logging providers. It acts as a wrapper for all the logger providers registered to it and a logger it creates can write to all the logger providers at once.


2 Answers

I'm not sure if I am missing something but don't you just want to raise the log level for the Microsoft logs?

Edit appsettings.json (assumes .AddJsonFile("appsettings.json", ...))

{   "Logging": {     "IncludeScopes": false,     "LogLevel": {       "Default": "Trace",       "System": "Information",       "Microsoft": "Information" 

To

{   "Logging": {     "IncludeScopes": false,     "LogLevel": {       "Default": "Trace",       "System": "Information",       "Microsoft": "None" 

Or the same modification via environment variables (assumes .AddEnvironmentVariables())

Logging:LogLevel:Microsoft=None 

You can also be more specific, the following reduces most entries but leaves Microsoft.AspNetCore.Hosting.Internal.WebHost at Information.

"Microsoft": "Information",   "Microsoft.AspNetCore.Mvc.Internal": "Warning", "Microsoft.AspNetCore.Authentication":  "Warning" 

Appologies if this doesn't work for log4net

like image 66
KCD Avatar answered Sep 22 '22 02:09

KCD


What have really worked for me was adding this in ASP.NET Core 2.0 project's Startup.cs file:

using Microsoft.Extensions.Logging; public void ConfigureServices(IServiceCollection services) {     .     .     .      services.AddLogging(     builder =>     {         builder.AddFilter("Microsoft", LogLevel.Warning)                .AddFilter("System", LogLevel.Warning)                .AddFilter("NToastNotify", LogLevel.Warning)                .AddConsole();     }); } 

This way you'll only get Warning level logs for logging info starting with the filters passed to builder.AddFilter.

My log4net.log file now doesn't show that huge amount of INFO logging spit by Microsoft and others.

More info here @ Microsoft Docs: Log filtering

like image 27
Leniel Maccaferri Avatar answered Sep 26 '22 02:09

Leniel Maccaferri