0

I am trying to add serilog to my api (asp net core) and I can not do it as I want. I have 2 examples: 1) This example works as I want, I only write in the ".txt" file what I need but I can not get it to work from a Controller

//Program.cs
public static void Main(string[] args)
{
    var logger = new LoggerConfiguration()
        .ReadFrom.Configuration(new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build())
        .CreateLogger();

    logger.Information("Hello, world!");

    CreateWebHostBuilder(args).Build().Run();
}

//Output

2018-07-02 18:17:18.210 -03:00 [INF] Hello, world!

2) This example works from a controller, but every time I want to write in the ".txt", it writes all the output of the VisualStudio console.

//Program.cs
public static void Main(string[] args)
{
    Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(new ConfigurationBuilder()
        .AddJsonFile("appsettings.json")
        .Build())
    .CreateLogger();

    CreateWebHostBuilder(args).Build().Run();
}

//Startup.cs
loggerFactory.AddSerilog();

//Controller
private readonly ILogger<ValuesController> _logger;

public ValuesController(ILogger<ValuesController> logger)
{
    _logger = logger;
}

[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
    _logger.LogInformation("Test");
    return new[] { "value1", "value2" };
}
1
  • 2
    Follow the setup instructions. You will need to set up the Serilog provider first in order to have the ASP.NET Core logging go to Serilog. Commented Jul 3, 2018 at 7:01

1 Answer 1

1

If you check the output .txt, will there be any log? I suggest you follow steps below to modify your project.

  1. Comment out loggerFactory.AddSerilog(); which is pointed at Serilog
  2. Run install-package Serilog.Sinks.File which is used to write log to file.
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.