Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can logging messages from asp.net core application in specific file on linux?

I installed an ASP.NET Core application as a Linux daemon. By default .NET writes logs into /var/log/message but I would like the application to write its logs in a specific file and store it permanently.

How I can do this? The application uses Microsoft.Extensions.Logging.

like image 814
Mikhail Avatar asked May 23 '18 08:05

Mikhail


2 Answers

Logs go where you tell them to go, not a specific location. /var/log/message is the location used by the Debug Provider only.

You need to specify which logging providers you want to use in the logging configuration of your project, as shown in the Logging in .NET Core article.

There's no built-in file provider and there's an ongoing discussion about whether there should be one. Logging to files isn't as simple as it sounds at first - how should the messages be formatted? What about rolling files? File names? Perhaps you want errors and verbose messages to be written to different files? Or use different files per subsystem/area/logger/business in your application, eg one log for the sales service, another to log external service calls?

There are a lot of good open source logging libraries, like Serilog and NLog that address these concerns. Some of the most popular ones are listed in .NET Core's documentation. .NET Core's logging can be configured to easily use those libraries.

For example, you can add a Serilog file target with the appropriate package.

To use it, you need to add a reference to the package, eg in your csproj file :

<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" />

And add it to the logging configuration with a call to AddFile :

WebHost.CreateDefaultBuilder(args)
    .ConfigureLogging((hostingContext, logging) =>
    {
        logging.AddFile("path/to/Logs/myapp-{Date}.txt");
    })
    .UseStartup<Startup>()
    .Build();

The {Date} argument is used to create rolling log files per date.

You can combine multiple loggers,eg you can write to the console with logging.AddConsole() or keep writing to /var/log/message during development with logging.AddDebug();

You can find many logging providers is you search for Extensions.Logging in NuGet.

You could also integrate other libraries like Serilog in your application and use their loggers. For example, you can use the Serilog.Sinks.Fluentd package to target Fluentd

like image 189
Panagiotis Kanavos Avatar answered Oct 05 '22 09:10

Panagiotis Kanavos


By default dotnet write logs into /var/log/message

Only if you use the "Debug logging provider" that simply logs calls to Debug.WriteLine. These calls are ignored in "release" builds, the compiler simply removes them from your app unless you have the DEBUG build constant.

Now back to the question: I was also kinda surprised there's no built-in logging in .NET 5 for Linux.

Especially considering that Linux has had great built-in logging tools for decades. Like syslog for example. Just send stuff to the /dev/log socket and linux will take care of the rest - write to a file, rotate logs, etc etc.

So I wrote a (very!) short C# file that just calls the built-in syslog() function from libc using DllImport.

It's not a huge Nuget dependency, not a "logging provider", not a DI-service or whatever. Just one 1kb C# file you can throw into your project and log stuff like this:

Syslog.Write(Syslog.Level.Warning, "MyAwesomeApp", "something went wrong");

Here's the gist https://github.com/jitbit/SyslogCore

P.S. This does not directly answer the question, but this page keeps popping up when I search for ways to log under Linux in .NET 5. So I decided to post another answer.

like image 31
Alex from Jitbit Avatar answered Oct 05 '22 09:10

Alex from Jitbit