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

MicrosoftILoggerTarget - Add support for override LoggerName when using ILoggerFactory #476

Merged
merged 1 commit into from
Feb 25, 2021

Conversation

snakefoot
Copy link
Contributor

@snakefoot snakefoot commented Feb 25, 2021

Then output from NLog Loggers can be redirected to match function-name:

//setup target to forward NLog logs to ILogger
//NOTE: string passed to CreateLogger must match "function.<function name>" or logs will not show in Log Stream
var msLoggerFactory = serviceProvider.GetService<ILoggerFactory>();
var msLogger = msLoggerFactory.CreateLogger("Function.TestLog");
var nLogToMsLogTarget = new MicrosoftILoggerTarget(msLogger) { LoggerName = "${mdlc:FunctionName}" };

And one can then forward the ILogger-name to NLog like this:

    [FunctionName("TestLog")]
    public static async Task<IActionResult> TestLog([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
                                                HttpRequest req, ILogger log)
    {
        using (NLog.MappedDiagnosticsLogicalContext.SetScoped("FunctionName", log.ToString()))
        {
           //shows in both Application Insights and Log Stream.
           log.LogInformation("Log From ILogger");

           //shows in Application Insights but not in Log Stream.
           _logger.Info("Log From NLog");

           return new OkObjectResult("OK");
        }
    }

See also: https://stackoverflow.com/questions/66357471/nlog-in-azure-function-log-streams-using-nlog-config

@snakefoot snakefoot added this to the 1.7.1 milestone Feb 25, 2021
@snakefoot snakefoot merged commit 3c672de into NLog:master Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant