1
votes

I'm using Nlog with Application Insights as Target in a WebJobs project to log telemetry. Everything seems to be working if I log just the message like below.

_logger.Log(LogLevel.Info, "Job completed");

I can see the Trace Info in application insights with the message "Job completed"

But I wanted to log some parameters along with the message like below.

_logger.Info($"Job created successfully", req.UserId, req.ReportName, jobId, searchString);

or like below

catch (Exception ex)
{
    _logger.Error(ex, "Error creating the job", req.UserId, req.ReportName, searchString);
    throw;
}

I'm expecting a trace in application insights containing the parameters which I passed along with the message. But I can only see the message but not any parameters or exceptions details.

What am I missing?

Edit:

NLog nuget version 4.3.8 and Microsoft.ApplicationInsights.NLogTarget nuget version 2.4.1

More Code:

try
{
    var jobId = _reportingService.RequestReport(req.ReportName, searchString).Result;

    _logger.Info($"Job created successfully", req.UserId, req.ReportName, jobId, searchString);

    var output = new RetrieveReportDataRequest()
    {
        CreationRequest = new ReportCreationRequest()
        {
            ImmutableUserId = req.UserId,
           ...
        },
        SearchParameters = searchString,
        JobId = jobId,
        Created = DateTime.UtcNow,
    };

    outputQueueMessage = JsonConvert.SerializeObject(output, settings);
}
catch (Exception ex)
{
    log.WriteLine(ex.Message);
    _logger.Error(ex, "Error creating the job", req.UserId, req.ReportName, searchString);
    throw;
}
2
it's a webjob projects? could you please add more code? and the nuget package you're using include the version. thanks.Ivan Yang
@IvanYang I have edited the question with more details.PNDev
Hello, you're using webjobs nuget package 3.0.x or 2.x?Ivan Yang
@IvanYang Webjobs nuget version is 2.1.0PNDev
@PNDev you can implement your own logger, as we do in our workspace. If you don't know how let me know I can supply example as answermaytham-ɯɐɥʇʎɐɯ

2 Answers

1
votes

You can take use of LogEventInfo, then add the parameters in it's Properties.

Sample code:

For log level info:

            LogEventInfo eventInfo = new LogEventInfo(LogLevel.Info, "event1", "this is a info111");
            eventInfo.Properties["myname"]= "myname is ddd";
            eventInfo.Properties["myid"] = "myid is ddd";
            eventInfo.Properties["myjobid"] = "myjobid is ddd";
            log.Log(eventInfo);

For error log level:

        #in you code, you can change the new Exception() to your own exception
        LogEventInfo eventinfo2 = new LogEventInfo(LogLevel.Error, null,null,null,null,new Exception("anexception222"));
        eventinfo2.Properties["errormessage"] = "thi si a error message";
        eventinfo2.Properties["myname"] = "myname is ddd";
        eventinfo2.Properties["myid"] = "myid is ddd";
        eventinfo2.Properties["myjobid"] = "myjobid is ddd";
        log.Log(eventinfo2);

Then you can see the parameters in azure portal:

enter image description here

1
votes

Another, maybe easier, option is to use structured logging.

e.g.

_logger.Info("Job {JobId} created successfully for {User} on {ReportName} with {Search}", jobId, req.UserId, req.ReportName, searchString);

This will create the event properties JobId, User, ReportName and Search.

See also NLog - How to use structured logging

note: So don't use interpolated strings in this case.