Skip to the content

The new way to handle error logging in Umbraco 9

Umbraco.Core.Logging has been replaced in Umbraco 9

With Umbraco 9 there is now no longer Umbraco.Core.Logging. This has been replaced and Umbraco is now using the standard Microsoft logging in .Net Core. The concept and most of the code structure is very similar but just a few subtle differences.

How to inject the logger to your controllers, helpers, and services

You can inject the logger in a very similar way to like you could do with Umbraco 8. The main difference is that you wont be injecting the logger from the Umbraco.Core.Logging namespace but instead from Microsoft.Extensions.Logging namespace.

The first think you need to do is add a using statement at the top of your controller or wherever you want to inject the logger.

using Microsoft.Extensions.Logging;

To inject the logger to your controller it can be done like this.

private readonly ILogger<HomePageController> _logger;
public HomePageController(ILogger<HomePageController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor) : base(logger, compositeViewEngine, umbracoContextAccessor)
{
     _logger = logger;
}

Then to call the logger you can see a demonstration in the following code. I have added both an error log and faked throwing an error. below that I have also demonstrated how to log information to the error log.

public override IActionResult Index()
{
     try
     {
          throw new Exception();
      }
      catch(Exception e)
     {
           _logger.LogError(e, "Index | Exception: {0} | Message: {1} | Stack Trace: {2}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "", e.StackTrace);
     }
     _logger.LogInformation("Some infortative message here");

     var vm = new HomePageViewModel(CurrentPage) { };
     return View("/Views/homePage.cshtml", vm);
}

If you are wondering where the error logs are now stored you will find them in the /umbraco/Logs folder.

The error logs can also still be views in Umbraco backend as shown in the screenshot.

Umbraco 9 Error Logging

To summerise Umbraco 9 error logging is very much the same apart from a couple of differences. The main one to take away is the fact we should now we using Microsoft.Extensions.Logging namespace.

 

* I just want to add one final note here since its an issue I got tripped up on. If your are like me and inject the ILogger into your controllers and custom services the please remember to inject it in the new way. If you compare this to Umbraco 8 you will notice the signature is slightly different.

private readonly ILogger<SiteHelper> _logger;
public SiteHelper(ILogger<SiteHelper> logger)
{
     _logger = logger;
}

 

 

About the author

David Armitage

.Net MVC Developer
.Net Core Developer
Umbraco Certified Master
Recruitment Professional

Hey Peeps,

I'm an entrepreneur and web developer with a passion for coding. I absolutely love working with Umbraco CMS and appreciate the Umbraco community even more.

I've got 10 years+ .Net experience and 7 years+ experience working in the recruitment industry, both coding and marketing job websites. I wanted to use my skills to help give something back to this awesome community and so I decided to build UmbraJobs.com.

My vision & fundamentals

I want this website to be a place where Umbraco Professionals and Umbraco Partners can advertise their services but at the same time I want to filter out all the under-qualified freelancers & agencies that take up the biggest part of other job & freelancer websites. We've all worked hard to gain our Umbraco certifications, partnership status, and help build the Umbraco community to what it is today. I want to see everyone get rewarded for their efforts.

Follow me on social media

If you're an Umbraco professional, partner, employer, recruiter or a passionate Umbraco community builder then I'm more than happy to hear from you. Follow me on my social media channels and please reach out if you have any needs, want help or consultation with anything Umbraco related or just want a general chat.

comments powered by Disqus

Blog Filter


How we can help?

Need help with an Umbraco project?

Need help with a project but not too sure who to approach? Send us your project brief and budget. We will provide a free consultation and can help you gather quotes from the best and most suitable freelancers or agencies.

Looking to hire an Umbraco Professional?

Have you got job vacancy and want to hire an Umbraco professional? Post a job on our website for free. Alternatively let us know your requirements and we will send suitable prospects your way.

Claim your free profile!

Are you an Umbraco Freelance Developer or Umbraco partner that wants to advertise on our site? If you work with Umbraco or registered as an Umbraco partner then you can create a profile for free.

Let's build the Umbraco Community

We're big on building the Umbraco community and we think you guys are awesome! If there's anyway at all we can help then please reach out.