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 · 1 comment
Labels
community To tag external issues and PRs

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
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
Projects
None yet
Development

No branches or pull requests

1 participant