Component/application area scoped logging #1896

Open
akovac35 opened this Issue Jan 10, 2017 · 14 comments

Projects

None yet

3 participants

@akovac35
akovac35 commented Jan 10, 2017 edited

Hello,

is there component based logging feature available with NLog; for example, can I specify:

logger name="MyNamespace.MyComponent" minlevel="Debug" writeTo="f1"

which would be equivalent to:

logger name="MyNamespace1.MyClass1" minlevel="Debug" writeTo="f1"
logger name="MyNamespace2.MyClass2" minlevel="Debug" writeTo="f1"
logger name="MyNamespace3.MyClass3" minlevel="Debug" writeTo="f1"

Thanks and regards,

Aleksander

Type (choose one):

  • Question

NLog version: 4.4.1

Platform: .Net 3.5

@304NotModified
Member

logger name="MyNamespace1.*" minlevel="Debug" writeTo="f1" ?

@akovac35

Hi,

thanks for your reply. So basically there is no component grouping option? What if I had very different namespaces as part of a component:

MyApp.Database
MyApp.Search
...

And say I wanted to include 15 namespaces under one component - I could not rely only on namespace patterns. Is there a way to change some code to have this feature?

@snakefoot
Contributor

Maybe you can use filters:

<logger name="MyApp.*" minlevel="Debug" writeTo="f1">
      <filters>
        <when condition="starts-with('${logger}','MyApp.Database')" action="Log"/>
        <when condition="starts-with('${logger}','MyApp.Search')" action="Log"/>
      </filters>
</logger>
@akovac35

Well both suggestions seem about the same - both require some sort of listing of included classes/namespaces/filters when specifying logger. It would be better if this could be preconfigured by developers, and the user would then only specify the component logger and not entire listing of loggers/filters.

Maybe this is a new feature rather than question.

@snakefoot
Contributor
snakefoot commented Jan 11, 2017 edited

would be better if this could be preconfigured by developers

You can modify the NLog-Configureation at runtime, and register your own LoggingRules that points to the wanted component-log-tagets.

@304NotModified
Member

Could you elaborate your example as I don't fully understand your question/Feature request?

@akovac35

So, in the initial post I asked if it is possible to specify only one logger, with some 'made up' name, and that would result in several loggers being enabled for different classes in different namespaces. Ofcourse, the list for the 'made up' name would have to be predefined by developers.

@snakefoot
Contributor
snakefoot commented Jan 11, 2017 edited

I asked if it is possible to specify only one logger, with some 'made up' name,

Possible solution is to create your own extension-method for LogManager:

NLog.Logger GetComponentLogger(this NLog.LogManager logManager, string componentName)
{
     var frame = new StackFrame(1, false);
     string loggerName = string.Concat(componentName, ".", frame.GetMethod().DeclaringType.FullName);
     return logManager.GetLogger(loggerName);
}
@304NotModified
Member

Has your question being answered?

@akovac35

Hi, I suppose some kind of extension would be necessary, just don't know if snakefoot's answer is correct - I am still in process of deciding which library to use for logging, so I haven't done any coding yet. So if I understand correctly, the componentName in the example comes from code, not from NLog config file? Well in that case the problem is not solved, because log specification should be able to combine component traces and class/namespace level traces.

@304NotModified
Member

So if I understand correctly, the componentName in the example comes from code, not from NLog config file?

There is some c# code needed, but it's generic and isn't listing the component names.

@akovac35

Well, maybe this could be made into a feature.

@snakefoot
Contributor
snakefoot commented Jan 19, 2017 edited

It would be better if this could be preconfigured by developers, and the user would then only specify the component logger and not entire listing of loggers/filters.

Well, maybe this could be made into a feature.

Well just create a static logger for each "component" you have, and then use the loggers where appropriate. Since you apparently don't want any registration in the config or runtime logic of your special "components"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment