2

I am testing some basic file logging with Asp.Net Core and I using Serilog as it's quick to setup.

The issue I having is when I log a message from the HomeController using the built in ILogger there is no mention of the controller in the file output. For example.. the logger is in the controller is like this and being injected the usual way in the constructor..

private readonly ILogger<HomeController> _logger;

I am am logging something as a test like this

_logger.LogError("Test error log");         

This appears in the file like this.. with no mention of HomeController.

2019-11-28T11:36:52.7735997+00:00 8000000c-0003-fe00-b63f-84710c7967bb [INF] test info log (72772865)

Also where/how do I format this date so it isn't so long? Its not clear from the docs if its done in the json, or startup.cs. And I dont need the 8000000a-0003-fe00-b63f-84710c7967bb request id.

In startup.cs this is set up as follows

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddFile("Logs/HPPartnerPortal-{Date}.txt");           

My config.json specifies the default of

"Logging": {
    "LogLevel": {
        "Default": "Information",
        "Microsoft": "Warning"          
    }

I notice you can add File logging using the configure method in your startup.cs as I did above.. or you can add it in program.cs as specified in the

https://github.com/serilog/serilog-extensions-logging-file

like this

Host.CreateDefaultBuilder(args)
            //we are setting up logging in Startup.cs so this is not needed ? !
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.AddConsole();
                logging.AddDebug();                 
                //logging.AddFile("Logs/myapp-{Date}.txt"); -- AddFile not recognised?!
            })

The problem here is it doesnt recognise AddFile even though I have added Serilog.Extensions.Logging.File This is 1.1.0 which is the latest version in nuget.

4
  • The best place is to check the Serilog documentation. Please check here Commented Nov 28, 2019 at 12:30
  • Sure, but that assumes you want to use their own static Log as opposed to ILogger. I think if its ILogger it must be specified in the config json under logging.. Commented Nov 28, 2019 at 12:48
  • Serilog is another logging framework as the default, used by ASP.NET Core out of the box. When you want to use Serilog, you have to initialze and configure it as described in the documentation. Commented Nov 28, 2019 at 13:08
  • I am going to update this question. Commented Nov 28, 2019 at 14:05

2 Answers 2

2

According to the documentation, this should set template in your appsettings.json

{
  "Logging": {
    ...
    "OutputTemplate" : "{Timestamp:HH:mm:ss} {RequestId,13} -[{Level:u3}] {Message} ({EventId:x8}){NewLine}{Exception}",
    ...
  }
}

I tried it but it didn't work for me. If anyone finds it working, please comment.

Edit. Found the solution, you must install 2.0.0 version of extension.

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

Comments

0

You can install the nuget package Serilog.Extensions.Logging or you can modify the Project.json and add the following line:

"Serilog.Extensions.Logging": "1.0.0"

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.