Asp.Net 5 (core) RC1: How to log to file (rolling file logging) [DNX Core 5 compatible solution]?
Asked Answered
U

2

6

How can I log to file in Asp.Net 5 RC1? I couldn't find any solution with Microsoft.Extensions.Logging. Is there any solution which is compatible with .Net Core 5 (DNX Core 5.0)? I also was trying to use Serilog but Serilog doesn't support core 5 yet.

Unproductive answered 27/1, 2016 at 13:6 Comment(2)
Did you try the NLog logger ?Copeck
Or you could go the 1984 route and just have a sub that writes a supplied log entry to a file of your choice and keep it rolling. If that is your only requirement it will work. Probably not preferred or recommended but certainly compatible and will work. If you need any example of such procedure let me know and I'll provide one.Michale
C
14

To use Serilog in your ASP.NET 5 RC1 project, add the following dependencies in your project.json file:

"Serilog.Extensions.Logging": "1.0.0-rc1-final-10092",
"Serilog.Sinks.RollingFile": "2.0.0-beta-465"

Create the logger in the Startup constructor:

public Startup(IApplicationEnvironment appEnv)
{
    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Debug()
        .WriteTo.RollingFile(Path.Combine(appEnv.ApplicationBasePath, "log-{Date}.txt"))
        .CreateLogger();
}

and add Serilog in the Startup.Configure method:

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    loggerFactory.AddSerilog();
Cork answered 28/1, 2016 at 3:58 Comment(4)
That works locally in VS, but I cannot get it to work on IIS even if the application itself works fine. Any idea why? No logging file is created.Calcine
Try giving the application pool identity permissions to write to the folder where you will be logging.Cork
also check seq (getseq.net) for displaying the logs in well formatted way in browser. (like elmah does).Bone
@JeffOgata I just did :: .WriteTo.RollingFile("logs\\log-{Date}.txt") and it works. Could you explain what is this :: "Path.Combine(appEnv.ApplicationBasePath,.." for ?Bone
T
0

Serilog.Extensions.Logging.File package is an easy way to add file logging to ASP.Net Core application (.NET Core 2.0 is supported in the latest version, which is pre-release at the moment).

  • Plugs in as ASP.NET Core logging provider
  • Provides a subset of Serilog functionality, specifically for logging to the file system.
  • Automatically pulls other Serilog packages as needed.

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

https://www.nuget.org/packages/Serilog.Extensions.Logging.File

Trot answered 22/6, 2018 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.