skip to Main Content

I wanted to add logs on the Azure Application Insights in my application, ASP.Net Web application MVC, .Net Framework is 4.6.2.

I do have added references to below nuget packages

  • Microsoft.ApplicationInsights
  • Microsoft.Extensions.Logging

I do have added below code in Global.asax.cs in the applicatin_start
TelemetryConfiguration.Active.TelemetryInitializers.Add(new MyTelemetryInitializer());

Inside HomeController.cs, I have added below code, but it is giving me below error at the line _logger.LogTrace()

Error

System.ArgumentNullException: ‘Value cannot be null.
Parameter name: logger’

Code

public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;
    public HomeController()
    {
        IServiceCollection services = new ServiceCollection();
        IServiceProvider serviceProvider = services.BuildServiceProvider();
        _logger = serviceProvider.GetService<ILogger<HomeController>>();
    }
    public ActionResult Index()
    {
        ViewBag.Title = "Home Page";
        _logger.LogTrace("Some trace message from Version 462");
        return View();
    }
}

Please let me know where I am going wrong.

Thanks,
Tushar

2

Answers


  1. Chosen as BEST ANSWER

    Below are the steps to resolve this issue.

    1. Navigate to your Project >> Add Application Insights Telemetry >> Application Insights Sdk (local) >> Next >> Finish >> Close.

    2. In the Global.asax.cs add below line

      TelemetryConfiguration.Active.TelemetryInitializers.Add(new MyTelemetryInitializer());

    3. Wherever wanted to add application insights add below code, you can create common logger file also.

          TelemetryClient client = new TelemetryClient(TelemetryConfiguration.Active);
          client.TrackTrace("Custom Track Trace");
      
          client.TrackAvailability(new AvailabilityTelemetry() { Name = "Availablilty Test"});
          client.TrackDependency(new DependencyTelemetry() { Name = "Dependency Test" });
          client.TrackEvent(new EventTelemetry() { Name = "Event Test" });
          client.TrackException(new ExceptionTelemetry() { Message = "Exception Test" });
          client.TrackMetric(new MetricTelemetry() { Name = "Metric Test" });
          client.TrackPageView(new PageViewTelemetry() { Name = "PageView Test" });
          client.TrackRequest(new RequestTelemetry() { Name = "Request Test" });
      

  2. Initially, you need to add Application Insights. Below are the steps you can follow to add Application Insights to your Application.

    1. Navigate to your Project >> Add Application Insights Telemetry >> Application Insights Sdk (local) >> Next >> Finish >> Close.

    2. You can register the initializer through applicationinsights.config file, while Adding Application Insights automatically we need to add the instrumentation key to ApplicationInsights.config before closing the </ApplicationInsights> tag file.

      <InstrumentationKey>"your-instrumentation-key-goes-here"</InstrumentationKey>    
      

      enter image description here

      OR

      protected void Application_Start()
      {
          // ...
          TelemetryConfiguration.Active.TelemetryInitializers.Add(new MyTelemetryInitializer());
      }
      

      And while logging in from your Home Controller try

      public class HomeController : Controller
      {
          ILogger<HomeController> _logger;
              public HomeController(ILogger<HomeController> logger)
              {
                  _logger = logger;
              }
      
              public ActionResult Index()
              {
                  _logger.LogInformation("Log message in the Index() method");
                  return View();
              }
      }
      
    3. After updating the Nuget packages, you can run your application where you can observe that telemetry will be submitted to Application Insights when you navigate the site’s pages.

    If Adding Application Insights Automatically isn’t working, then you can try manually from HERE.

    REFERENCE :

    1. Configure monitoring for ASP.NET with Azure Application Insights
    2. azure – Application Insights for WebAPI application – Stack Overflow
    3. Logger.LogTrace() does not write message to application insights
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search