Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kevin Jump 2147 posts 13604 karma points MVP 6x c-trib
    Apr 28, 2021 @ 16:12
    Kevin Jump
    0

    Logging in Umbraco 9 - anyway to share the calls with Umbraco 8

    Hi,

    I am looking at migrating a package i have on v8 and wondering if i can do some multi-targeting in the project so i have one code base ?

    I have an example of this working for the linkedpages package. ( https://github.com/KevinJump/Our.Umbraco.LinkedPages/tree/dev/shared ) for a lot of not to "technical inside Umbraco stuff", you can get away with #ifdef blocks around using statements.

    e.g the controller for that package has :

    #if NETFRAMEWORK
    using System.Web.Http;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Models.Entities;
    using Umbraco.Web.Mvc;
    using Umbraco.Core.Services;
    using Umbraco.Web.WebApi;
    #else
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    
    using Umbraco.Cms.Core;
    using Umbraco.Cms.Core.Models;
    using Umbraco.Cms.Core.Models.Entities;
    using Umbraco.Cms.Core.Services;
    using Umbraco.Cms.Web.BackOffice.Controllers;
    using Umbraco.Cms.Web.Common.Attributes;
    #endif
    

    So i was going to try this approach on another project, but i have quite a few logging lines, through the code (it really helps to have logging in packages, for when people tell you they are having issues, but you do not have access to their site!).

    So for example i will inject ILogger (from Umbraco.Core.Logging). into a service and call the logger.

    logger.Debug<MyService>("Something logged {value}", value); 
    

    in v9 you inject ILogger<MyService> (from using Microsoft.Extensions.Logging;) and the call is

    logger.LogDebug("Something logged {value}", value); 
    

    now if i have shared code across a project it would be great if i could have the same logger lines in the code for both versions (and #ifdef around each log line seems fugly)

    real questions are :

    • can i / should i go to the base serilog interface and use that logger which i am guessing is the same between v8 and v9 ?
    • should i look at wrapping the logging so the same call can be made in each version -and the diffrences are in a method somewhere else ?

    is there some super neat trick i am missing that would make this bit super simpler ☺

  • Bjarke Berg 29 posts 265 karma points hq
    Apr 28, 2021 @ 16:39
    Bjarke Berg
    0

    Hi Kevin..

    I think I would look into making two different extension methods that are named equally and with the same params, except the ILogger instance.

    Thereby it should even work with the same using statements. But would require you to inject the different ILogger types, in the ctor, but I guess the ctor is different anyway in most cases?

    Extension methods are also used in this article about high performance logging: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/loggermessage?view=aspnetcore-5.0

    That is very likely something we will look into in the CMS too, to improve logger performance.

    BR/ Bjarke Berg

  • Benjamin Carleski 33 posts 291 karma points MVP c-trib
    Apr 28, 2021 @ 16:57
    Benjamin Carleski
    0

    One other thought I had is what if we (meaning the Umbraco runtime) injected a custom Microsoft ILogger instance in the v8 version, so that in both versions you could use the Microsoft ILogger instance. The custom instance would handle logging in v8 as you would expect, and in v9 it would work with the native instance.

  • Bjarke Berg 29 posts 265 karma points hq
    Apr 28, 2021 @ 16:58
    Bjarke Berg
    0

    Sounds like a good idea..

  • Benjamin Carleski 33 posts 291 karma points MVP c-trib
    Apr 28, 2021 @ 17:07
    Benjamin Carleski
    0

    I've opened a feature request at https://github.com/umbraco/Umbraco-CMS/issues/10196 to see if we can get this into v8.

Please Sign in or register to post replies

Write your reply to:

Draft