C
C#•6mo ago
Poller

ILogger<T>

We currently think about removint a lot of direct NLog references as preparation for future projects. Now we have some Class A that instanciates at points in Time B. But at CreationTime of T we dont know yet all informations about the category to use. i cant imagine we are the first one to be in this position.
39 Replies
jcotton42
jcotton42•6mo ago
Inject ILoggerFactory instead.
jcotton42
jcotton42•6mo ago
ILoggerFactory Interface (Microsoft.Extensions.Logging)
Represents a type used to configure the logging system and create instances of ILogger from the registered ILoggerProviders.
jcotton42
jcotton42•6mo ago
Though, I wonder why your log categories vary so much.
Poller
PollerOP•6mo ago
sorry i did miss a point in my question. B never knows the information. Only A knows it. now we used NLog-`Properties They are a lot of TCP Clients and we want/need to be able do identify them. but we know the resulting user id only after a few messages and only from the outside. we found Scopes. a coworker currently does tests with that. if they are able to be used there.
tera
tera•6mo ago
definitely can use scopes this is not something to use categories for if you send your logs to something that supports structured logs (e.g. seq), you can see and query all that data asp net core uses scopes to add request id, connection id,.. etc to logs
Poller
PollerOP•6mo ago
but as we see, if i use a dictionary in scopes, its just .ToString() and NLog does show the values. thats just NLog specific? (i.E.: whats the intent of the dictionary thats shown all over the docs. is it just for structured logging? )
tera
tera•6mo ago
what dictionary? ahhh i mean you could configre it to show in every log message..
Poller
PollerOP•6mo ago
we ran into more issues. i try to verbalize it: - we have 1 place (Tcp listener (simplified), where we create some clients(class) - there we know this user id. - we want to identify in log what class for what userid the log is from. - we have this code used in 'legacy' but also work on a azure implementation. so we need to kind of spread our legs here to archieve all... (but also dont want to do tooo much custom stuff/workarounds)
tera
tera•6mo ago
where do you log to
Poller
PollerOP•6mo ago
legacy uses nlog into file. azure for now uses the console (not decided if we want more later)
tera
tera•6mo ago
Poller
PollerOP•6mo ago
thats a doc i have seen. the scopes in console look like just 'ToString' done onto the dictionary. thats currently where its a bit of area where we are not sure how and what to do. (trying to do the right thing here).
tera
tera•6mo ago
but to answer here.. you can put any object and use it to add arbitrary data to scope. ofc usually its something simple as dict, list, string, but could be a class too
Poller
PollerOP•6mo ago
1 sec, i get a log example in a moment.
tera
tera•6mo ago
now if you still use nlog (i thought you already migrated) then idk if it will be nlog specific
Poller
PollerOP•6mo ago
nlog is in legacy where it works. for the azure implementation we want to ditch the NLog dependency. thats an example of the console: fail: ConsoleApp2.LogInitializer[0] => System.Collections.Generic.Dictionary`2[System.String,System.Object] With just name
tera
tera•6mo ago
yea so how is that logged in code
Poller
PollerOP•6mo ago
we have a bit of unexpected time on our hands, so we decided we invest a bit into learning/researching for our future log situation.
tera
tera•6mo ago
def gonna be worth
Poller
PollerOP•6mo ago
thats the relevant lines var logger = logFactory.CreateLogger<LogInitializer>(); var logProperties = new Dictionary<string, object>(); logProperties["Name"] = "Michael"; using (logger.BeginScope(logProperties)) logger.LogError("With just name"); the next thing would be some nested object, on how to give it the correct scope, since it doesnt and should not know about that (a simple parser for example doesnt need some userid or such (i hope i can articulate clear what i mean - english is not 1st language.) thats the goal 🙂 what i postet is just testing and sandbox code. to isolate the behaviour
tera
tera•6mo ago
logger.BeginScope("something something {Foo}", logProperties) probably need a template
Poller
PollerOP•6mo ago
i try in 1 moment. i now get the project for the tests sent to me 🙂
tera
tera•6mo ago
cool
Poller
PollerOP•6mo ago
this topic sparked my personal interest more than i could ever admit to my wife
tera
tera•6mo ago
😆
Poller
PollerOP•6mo ago
i will quickly take a look into templates for console log.
tera
tera•6mo ago
i meant your code in .BeginScope is missing template string argument so i gave an example how to add
Poller
PollerOP•6mo ago
ahhh i understand what you mean.
tera
tera•6mo ago
https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loggerextensions.beginscope?view=net-8.0 public static IDisposable? BeginScope (this Microsoft.Extensions.Logging.ILogger logger, string messageFormat, params object?[] args); yea messageFormat, template whatever
Poller
PollerOP•6mo ago
"{}" as template... works like a charm 🙂 -top is console - bottom is NLog
No description
Poller
PollerOP•6mo ago
thank you so much for your help. this archieves exactly what we need
tera
tera•6mo ago
{} works but this means if you ever send that log to something that ingests structured logs, those values wont have a "key" generally no harm to just put something like {ConnectionProperties} but ye np
Unknown User
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Poller
PollerOP•6mo ago
thanks @TeBeCo im gonna look into this enritcher later today.
Unknown User
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Poller
PollerOP•6mo ago
it does. i think i will build a mvp project and document this maybe this evening. this shows maybe the full intent
Unknown User
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Poller
PollerOP•6mo ago
the current increment we dont have. we just have 10 liner tests of features (maybe also a wrong approach)
Unknown User
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server