❔ Log from helper class without creating new context? (Serilog)

I have a bunch of services with their own dedicated loggers that have config'd settings for filtering based on that exact type:
{
"Name": "Logger",
"Args": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": { "expression": "SourceContext = 'FooNamespace.BarClass'" }
}
]
}
}
{
"Name": "Logger",
"Args": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": { "expression": "SourceContext = 'FooNamespace.BarClass'" }
}
]
}
}
This way, I can setup output log files for each service individually. Now I have a helper class that all of the services utilize, and I want to add logging to this helper class, but I want the logs to appear in the log file that the calling service already has going. Since the filtering above is based on type, I'm not sure how to wire this up. Right now my helper class is setup to use DI like:
public class Helper
{
private readonly ILogger<Helper> _logger;
public Helper(ILogger<Helper> logger) => _logger = logger;
}
public class Helper
{
private readonly ILogger<Helper> _logger;
public Helper(ILogger<Helper> logger) => _logger = logger;
}
But this seems to create a new logger for Helper specifically, which isn't caught by the calling service's logger filtering.
4 Replies
phaseshift
phaseshift2y ago
Seems like you should di 'your logger<T>' into the classes instead of logger<T>. Unless you can modify the logger factory somehow
fenixblades
fenixblades2y ago
i don't follow. my entrypoint DI is setup like this
Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =
{
services.AddHosted<ServiceA>();
services.AddHosted<ServiceB>();

services.AddScoped<Helper>();
}
Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =
{
services.AddHosted<ServiceA>();
services.AddHosted<ServiceB>();

services.AddScoped<Helper>();
}
and each service and helper has the standard ILogger<T> ctor parameter, where T is that service/class name
phaseshift
phaseshift2y ago
Maybe simplest to just provide method overloads that take an Ilogger, and pass the logger in from your other services
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Swagger Open API not showing Request examples in the UII've written an Azure Function with Open API in C#, but when I open up the Swagger/UI to test my AzuLooking solution for get query from _dbContext.SaveChangesAsync() for save logs and easier debuggingHello, i have a .net core 6.0 app that save entites in MSSQL database with EfCORE 6.0 so my function❔ I'm building an MVC core webshop. How should I be structuring controllers, actions and views?So, right now, I have one single view, and this view renders both information about the product and ❔ How to make an object follows my mouse?so I am new at programming and trying to make a top down shooting games in windows form app. My lect❔ Is this the right way to implement an Interface```csharp interface IObstacles { void Draw(Graphics g); } class Redbox : IO❔ Is there any tool converting csv file to Class (seeding data)?I am using EF to seed data and I have an excel file containing like 500 records. If that's around 1❔ Is it possible to have WPF window hidden until Blazor WebView loads page?I'm trying to hide a WPF window until the Blazor WebView loads, but it seems that the initialization❔ SQLite in-memory Database read from ReadOnlyMemory bytesI'm receiving an SQLite database payload from an AMQP-0-9-1 message in the form of `ReadOnlyMemory<bSettings.Default not savingI have two integers, primary and secondary, that i want to persist, but they refuse to❔ need help opening siteshow do i make visual studio open a site and then press on a bar to type something