0

I created my own custom loger provider FileLogerProviderr to save logs to file.

I added custom FileLoggerProvider in program.cs:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureLogging((hostingContext, logging) => 
                {

                    logging.AddProvider(new FileLoggerProvider(Path.Combine(Directory.GetCurrentDirectory(), "logger.txt")));
                    logging.SetMinimumLevel(LogLevel.None);

                })
                .UseStartup<Startup>();

To test I added log in controller:

public class HomeController : Controller
    {
        private ILogger<HomeController> logger;

        public HomeController(ILogger<HomeController> log)
        {
            logger = log;
        }
        public IActionResult Index()
        {
            logger.LogDebug($"test index path!!!");
            return View();
        }
}

Working, But in file except test index path I see many additional information. I dont need this information. I need only save in file my log from controller.

Request starting HTTP/1.1 GET http://localhost:44339/  
Route matched with {action = "Index", controller = "Home", page = "", area = ""}. Executing action LoggingTest.Controllers.HomeController.Index (LoggingTest)
Executing action method LoggingTest.Controllers.HomeController.Index (LoggingTest) - Validation state: Valid
test index path!!!
Executed action method LoggingTest.Controllers.HomeController.Index (LoggingTest), returned result Microsoft.AspNetCore.Mvc.ViewResult in 11.8159ms.
Executing ViewResult, running view Index.
Executed ViewResult - view Index executed in 43.4928ms.
Executed action LoggingTest.Controllers.HomeController.Index (LoggingTest) in 224.1184ms
Request finished in 400.2906ms 200 text/html; charset=utf-8
Request starting HTTP/1.1 GET http://localhost:44339/favicon.ico  
Sending file. Request path: '/favicon.ico'. Physical path: 'C:\Users\dborovsky\projects\LoggingTest\LoggingTest\wwwroot\favicon.ico'
Request finished in 39.8394ms 200 image/x-icon

How I can solve this? Thank you

UPDATE appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "CONSTRING"
  },
  "Logging": {
    "LogLevel": {
      "Default": "None",
      "System": "None",
      "Microsoft": "None"
    }
  },
  "AllowedHosts": "*"
}
3
  • Possible duplicate of: stackoverflow.com/questions/35251078/… If you don't think it is, please post your appsettings.json file Commented May 28, 2019 at 4:33
  • i have already updated with appsetings.josn. check please. I just set None. Commented May 28, 2019 at 4:41
  • 1. You didn't add a logging configuration by logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); 2. Make sure you've changed the appsettings.Development.json (or appsettings.Production.json if in production environment). Commented May 28, 2019 at 5:26

1 Answer 1

1

Maybe you´ll like to use a configuration to prevent all events gets logged.

I recommend you inject the logger in ConfigureServices (.ConfigureLogging from Program.cs at least you want to log web hosting events):

public void ConfigureServices(IServiceCollection services)
        {

            services.AddLogging(loggers=> 
            {
                loggers.AddConsole();
                loggers.AddDebug();
                loggers.AddProvider(new FileLoggerProvider(Path.Combine(Directory.GetCurrentDirectory(), "logger.txt")));
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

Then you´ll can configure as follows (make sure you have the enviroment variant properly configurated):

{
  "ConnectionStrings": {
    "DefaultConnection": "CONSTRING"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "None",
      "Microsoft": "None"
    }
  },
  "AllowedHosts": "*"
}

And the implementation:

public class HomeController : Controller
    {
        private ILogger<HomeController> logger;

        public HomeController(ILogger<HomeController> log)
        {
            logger = log;
        }
        public IActionResult Index()
        {
            logger.LogDebug($"test index path!!!");
            return View();
        }
}

Here is as pretty nice Microsoft´s article about logging:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.