how to create separate log files per thread using nlog?

Try doing this instead (Using BeginScope to inject ScopeFileName-property into NLog MDLC):

[HttpPost]
public async Task<IActionResult> Index([FromBody] JsonElement requestElement)
{
    try
    {
        var myJsonString = JObject.Parse(requestElement.GetRawText());
        var fileName = myJsonString["fileName"].ToString();

        // Pushing input-filename as scope-property, accessible from MDLC
        using (this._logger.BeginScope(new [] { new KeyValuePair<string, object>("ScopeFileName", fileName) }))
        {
            for (int i = 0; i < 10; i++)
            {
                // REMOVED AS BROKEN -> await this.StartJobLogger(fileName);

                this._logger.LogInformation($"File: {fileName}");
                await Task.Delay(1000);
            }

            return this.Ok("done");
        }
    }
    catch (Exception e)
    {
        throw;
    }
    finally
    {
    }
}

But notice that taking user-input and using as filename, can be used as an attack-vector.

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Info">

    <!-- enable asp.net core layout renderers -->
    <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
    </extensions>

    <variable name="ScopeFileName" value="${mdlc:ScopeFileName:whenEmpty=Navigator}" />

    <!-- the targets to write to -->
    <targets async="true">
        <!-- write logs to file  -->
        <target
            xsi:type="File"
            name="allfile"
            fileName="c:/temp/${ScopeFileName}-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|TraceIdentifier: ${aspnet-TraceIdentifier}|SessionId: ${aspnet-sessionid}|${message} ${exception:format=tostring}" />
        <target
            xsi:type="Console"
            name="detailConsole"
            layout="${level:truncate=4:lowercase=true}: ${aspnet-sessionid} - ${logger}[0] ${newline}      ${message}${exception:format=tostring}" />
    </targets>

    <!-- rules to map from logger name to target -->
    <rules>
        <!--All logs, including from Microsoft-->
        <logger name="*" minlevel="Trace" writeTo="allfile" />
        <logger name="*" minlevel="Trace" writeTo="detailConsole" />
    </rules>
</nlog>

See also: https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-properties-with-Microsoft-Extension-Logging

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top