0

I have some trouble by configuring the loggers in my ASP.Net Core server application to create different log files for some projects.

Solution:

+ MyProject.Core (Library)
+ MyProject.DeviceApi.Controller (Library)
+ MyProject.WebsiteApi.Controler (Library)
+ MyProject.Server (ASP.NET Core)

MyProject.Server depends on MyProject.DeviceApi.Controller and MyProject.WebsiteApi.Controller.
MyProject.DeviceApi.Controller depends on MyProject.Core.
MyProject.WebsiteApi.Controller depends on MyProject.Core.

Services in MyProject.Core using loggers as interface. Services containing logging statements don't have to be registrered as singeltons

The MyProject.Server project registers controllers, items for di and defines some background task (like scheduled jobs).

There are two separate controller projects because they are independend, each contains many controllers and they should be logical devided.

What I want

  • Server.log (Contains entries triggered by Operations directly in MyProject.Server)
  • DeviceApi.log (Contains entries triggered by operations directly in MyProject.DeviceApi.Controller)
  • WebsiteApi.log (Contains entries triggered by operations directly in MyProject.WebsiteApi.Controller)

Currently I have no custom configuration, because all atemps failed to get the desired result.
I'm also fine using a logging dependncy like nlog.

But as far as I know I have to create separate instances for each service in MyProject.Core using a different logger instance.

D. Weber
  • 503
  • 5
  • 21
  • 1
    I'd suggest a slightly different approach: In general, your code should not contain things like "log this to file xy" - how logging works depends on where the app runs and how your logging infrastructure looks like. Multiple files are great, but what if you "upgrade" to some kind of log analytics or want to switch to kubernetes? You could also log everything "normally" and configure the logger output to split it to separate files: https://stackoverflow.com/questions/28292601/serilog-multiple-log-files - now you can adjust the format whenever you need without changing your code... – Christoph Lütjen Nov 10 '20 at 15:22
  • Yes, a kind of microserves would be the better approach, specially with regrad to dockerization, microservices etc. Sadly it was not my decision to create a monolith. And Yes, code in libraries should not define loggers and to configure the logger to log in different files should be the way to go. I know how to filter for namespaces, but not for "called by namespace" (or somethink like that) – D. Weber Nov 10 '20 at 18:31
  • Assuming you use the logger by injecting it like so `ctor(ILogger logger)` you should have everything you need? MyController is inside of one of your apis and you can filter by namespace. – Christoph Lütjen Nov 10 '20 at 19:29
  • 1
    Ah - I see, you want to split by api but that should include logging from libs. You can enrich serilog logging context, see https://github.com/serilog/serilog/wiki/Enrichment - this allows you to add e.g. your "containing project" or hostname. If hostname solves your problem there are prebuild packages to do this, e.g. https://github.com/IharYakimush/serilog-enrichers-asp-net-core-http-context (not tested). I'm always talking about serilog here because that's what we're using. Not sure what's possible with the "default" logger. – Christoph Lütjen Nov 10 '20 at 19:38
  • okay, i will take a look at it. thanks for your suggestions :) – D. Weber Nov 10 '20 at 21:31

0 Answers0