Copied to clipboard

Flag this post as spam?

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


  • Venkat 6 posts 97 karma points
    Jan 19, 2024 @ 07:05
    Venkat
    0

    Writing information and exceptions from razor view to logs

    Hello Team,

    I am very new to umbraco i have installed Umbraco 12 version all iam going good but now i got stucked with logs

    I have a razor view which is serving with content publish model and i don't have any controllers in my solution so now i need to log some information and which i can see in umbraco backoffice i used below syntax to write information log

    @using Microsoft.Extensions.Logging
    @inject ILogger logger
    
    @{    
        logger.LogInformation("Hello, this is a test.");
    
        try
        {
             logger.LogInformation("Hello, this is a test. Log is working");
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "An error occurred.");
        }
    }
    

    I have tried by registering service in start.cs and it looks like below

     public void ConfigureServices(IServiceCollection services)
            {
                services.AddUmbraco(_env, _config)
                    .AddBackOffice()
                    .AddWebsite()
                    .AddDeliveryApi()
                    .AddComposers()
                    .Build();
                services.AddLogging();
            }
    

    Can anyone please help me on this i stuck with since 8 hrs

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Jan 19, 2024 @ 22:32
    Marc Goodson
    102

    Hi Venkat

    I think you just need a c# class when you inject the logger into the razor view

    Eg

    @inject ILogger<UmbracoViewPage> _logger

    I don't think you need to do anything in configure services...

    Regards

    Marc

  • Venkat 6 posts 97 karma points
    Jan 22, 2024 @ 07:28
    Venkat
    1

    Hi Marc Goodson,

    I wanted to express my sincere gratitude for your prompt and helpful response to my question. Your solution worked perfectly for me, and I truly appreciate the time and effort you put into assisting me.

    Thank you once again for your invaluable support!

Please Sign in or register to post replies

Write your reply to:

Draft