Copied to clipboard

Flag this post as spam?

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


  • Oskar 5 posts 75 karma points
    Nov 01, 2019 @ 16:25
    Oskar
    0

    Good morning, I am begginer using Umbraco, I would like knowing about logging in umbraco. I want to do custom logging for my site... what things should I consider for that? .. I hope any one can help me.. Thanks.

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Nov 02, 2019 @ 21:02
    Marc Goodson
    0

    Hi Oscar

    There is a good introduction here to serilog which Umbraco has baked in:

    https://our.umbraco.com/Documentation/Getting-Started/Code/Debugging/Logging/

    Particularly how you can write your own custom messages to the log.

    Regards

    Marc

  • Oskar 5 posts 75 karma points
    Nov 05, 2019 @ 14:54
    Oskar
    0

    Thanks Marc.

  • Oskar 5 posts 75 karma points
    Nov 05, 2019 @ 15:06
    Oskar
    0

    Good morning All:

    I created a class and one method with this code :

    using Serilog; using Serilog.Sinks.Splunk;

    Log.Logger = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.EventCollector .WriteTo.File(.....) .CreateLogger();


    I want to use it in my controllers and I am doing this:

    using Umbraco.Core.Logging;
    
    public class UmbracoDefaultController : RenderMvcController
    {
        private readonly ILogger _logger;
    
        public UmbracoDefaultController(ILogger logger)
        {
            _logger = logger;
        }
    
       public ActionResult Index(BasicViewModel viewModel)
        {
             var testname = "Hello first test - 01";
             Logger.Info<UmbracoDefaultController>("Begin - {testname} ", testname); 
        }
    }
    

    Is it ok?...or Am I working in wrong way?.. Is there dependency injection? if there is no , How I could work using dependency injection using injectlight from umbraco?

    I hope someone can help me.

  • Craig100 1136 posts 2523 karma points c-trib
    May 23, 2020 @ 19:43
    Craig100
    0

    Hi Oskar,

    Did you get your example to work? When I use the docs like you have I just get "Logger does not contain a definition for Info". Which is true, it's "information" but then the syntax for the rest seems wrong.

    I'm trying to use it in my own class in a V8.6.1 .core project (as opposed to .web).

    Any advice would be great.

    / Craig

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    May 24, 2020 @ 10:51
    Nik
    0

    Hi Craig,

    Are you trying to create your own logging or use the Core Umbraco logging facilities?

    If it is the latter and you are taking advantage of Dependency Injection, if you inject ILogger or IProfilingLogger (both are in the Umbraco.Core.Logging name space) then you can access them and log things as and when you need.

    Both have .Info, .Warning, and .Error methods on them where you can pass the relevant information to the methods to be logged.

    Nik

  • Craig100 1136 posts 2523 karma points c-trib
    May 24, 2020 @ 12:22
    Craig100
    0

    Hi Nik,

    Thanks for your reply. I'm trying to use the Core Umbraco logging facilities. Although I've read and read about DI and understand what it's meant to do, I haven't yet managed to get my head around the syntax, it just doesn't say anything to me.

    The class is "using Umbraco.Core.Logging;" but on Logger.Info<ImportProperties>("Error Saving Property ID: {PropertyId}", apiProperty.property_ID); I just get "Logger does not exist in the current context". Do I "new" something or what is the syntax to DI it? It's so confusing.

    If I use the DI syntax from the Docs then I get "Logger does not contain a definition for Info".

    I'm "just" updating a class called by an API from V7 to V8 ;)

    The V7 code which worked was LogHelper.Error(MethodBase.GetCurrentMethod().DeclaringType, "Error Saving Property ID: " + property.Property_ID, ex);

    /Craig

  • Trevor Husseini 20 posts 151 karma points
    Sep 23, 2020 @ 03:27
    Trevor Husseini
    0

    Oskar,

    Did you or anyone else find an approach to this issue?

    Like Craig100, I too am struggling to wrap my mind around DI, especially in the context of moving from an ApplicationEventHandler in v7.4.2 to a Composer / Component in v8.6.2. Ultimately, my goal is to hook into Umbraco's messaging pipeline to display a custom message in the Backoffice when a user publishes a node that doesn't meet certain business rules.

    I guess the question I'm left with is "how do I get a handle on whatever Logger Umbraco is currently using that implements ILogger?"

    Here's a snippet of my DelegatingHandler, as is from the v7.4.2 implementation:

    public class WebApiHandler : DelegatingHandler
    {
        private readonly ILogger _logger;
    
        public WebApiHandler(ILogger logger)
        {
            _logger = logger;
        }
    }
    

    Here's where I get stuck in my Composer in the v8.6.2 implementation:

    public class WebApiComposer : ComponentComposer<WebApiComponent>, IUserComposer
    {
        public override void Compose(Composition composition)
        {
            System.Web.Http.GlobalConfiguration.Configuration.MessageHandlers.Add();
    
            base.Compose(composition);
        }
    }
    
    public class WebApiComponent : IComponent
    {
        public void Initialize()
        {
            // Method intentionally left empty.
        }
    
        public void Terminate()
        {
            // Method intentionally left empty.
        }
    }
    

    What do I pass to .Add()? I know I can create an empty constructor and create a brand new instance of Serilog, but doesn't that defeat the purpose of DI?

Please Sign in or register to post replies

Write your reply to:

Draft