Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Category name in Log event when no Logging framework used (Microsoft.Extensions.Logging) #2903

Open
dolivanu opened this issue Nov 26, 2024 · 3 comments
Labels
community To tag external issues and PRs feature request To tag an issue after triage that is a feature instead of TD

Comments

@dolivanu
Copy link

dolivanu commented Nov 26, 2024

Log events captured by .Net APM agent, when no Logging framework used (plain Microsoft.Extensions.Logging), does not include the logging Category name.

Feature Description

Log events captured when using plain Microsoft.Extensions.Logging, includes the following properties, taken from Console example Console example

entity.guid:Mz...
entity.guids:Mz...
entity.name:ConsoleSample
hostname:PC20
level:INFORMATION
message:Chilling for a few seconds...
newrelic.logPattern:nr.DID_NOT_MATCH
newrelic.source:logs.APM
timestamp:1732634056000

The most important log properties are level and message, captured by the .Net APM agent, but in some scenarios the log category, used tipically for filtering logs, is also useful as should contain the class name and allows locating the log source.

Logging frameworks tipically include the categoy name in the information dumped. For example, Microsoft Logging in the Console log formatting Simple includes the category name, in the exapmles provided shown as Program[0], Console log formatting

In the Console example linked, in the console output the log is as info: ConsoleSample.Worker[0] Chilling for a few seconds... whereas the ConsoleSample.Worker is the category name.

Describe Alternatives

You can always manually include in the log message the class name as the log source (ConsoleSample.Worker: part):
_logger.LogInformation("Console.Worker: Getting the weather forecast for Portland, OR");

The manual introduction of the class name as the category in every log message could be alleviated using the BeginScope() facility of Microsoft.Extensions.Logging, a real example of this usage pattern:

namespace Application.Loader.Files;
public class CheckFileLoader(ApplicationDbContext context, ILogger<CheckFileLoader> logger)
{
    public void Load(string path)
    {
        using var _ = logger.BeginScope("LogCategory={LogCategory}", GetType().Name);

        if (!File.Exists(path)) throw new FileNotFoundException(null, path);

        // Uses 'CheckFileLoader' as the category name manually added in every logger call through 'BeginScope()'
        // The log event captured by NR includes a 'context.LogCategory:CheckFileLoader' property
        logger.LogInformation("Loading file");
        ....
    }
}

Additional context

Log category names are defined when you create the logger. In the Console example linked. the logger received by the Worker class is defined as ILogger<Worker> logger, being <Worker> the simplest and most usual way to define the category name, using the class name.

Subsequent calls to the ILogger<Worker> logger should include in New Relic a category:Console.Worker property in the log event captured and sent to the NR1 platform.

As contextual logging is the common way to use the Microsoft.Extensions.Logging logging facilities, the category name is always defined and should not be part of the log message, thus NR should capture and include it in the log event as the log source.

Priority

Nice to have

@workato-integration
Copy link

@github-actions github-actions bot added the community To tag external issues and PRs label Nov 26, 2024
@nr-ahemsath
Copy link
Member

@dolivanu Thanks for submitting this. We were out on Thanksgiving break last week so apologies for the delay in responding. I should have time to take a look at this this week.

@jaffinito jaffinito added the feature request To tag an issue after triage that is a feature instead of TD label Dec 10, 2024
@jaffinito
Copy link
Member

I dug into our instrumentation, and it does appear that it would be possible for us to pull our the category name at the point that we instrument the logger. We have an open Feature Request to add support for reporting category with MEL, but I'm afraid I don't have an expected release window for you at the moment. We will update this ticket when we have more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community To tag external issues and PRs feature request To tag an issue after triage that is a feature instead of TD
Projects
None yet
Development

No branches or pull requests

3 participants