Copied to clipboard

Flag this post as spam?

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


  • Dibs 202 posts 991 karma points
    Apr 18, 2016 @ 15:16
    Dibs
    0

    Logging errors

    Dear Umbraco Team

    I have a created contact form surfaceController, all straight forward and working. I would like to add error logging to handle any exceptions that may occur with the contact form, i.e network goes down, server goes down application fault.

    I am new to MVC.net and not sure what best process to use to log errors to troubleshoot. i.e ELMAH or built in Umbraco logging ?

    Thanks Dibs

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Apr 18, 2016 @ 19:09
    Dan Diplo
    0

    I would use the built-in logging (which is built on top of log4net). Just add a reference to Umbraco.Core.Logging and then you can use:

    LogHelper.Error<YourClassName>("Your error message", ex);
    

    Where YourClassName is the name of the class you want to add to the log that is the calling class, and ex is the exception you wish to log.

    You can also do:

    LogHelper.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Your error message", ex);
    

    or

    LogHelper.Error(typeof(this), "Your error message", ex);
    

    And (shameless promotion!) you can use my custom log viewer extension to view logs from Umbraco - see https://our.umbraco.org/projects/developer-tools/diplo-trace-log-viewer/

  • Dibs 202 posts 991 karma points
    Apr 19, 2016 @ 08:10
    Dibs
    0

    Thanks Dan

    I have made use of Loghelper.Error within my controller, and logs error as expected. what i would like is errors raised within my views to be logged a well i.e syntax error within my razor. I lack experience with MVC .net and not sure how to log any errors from my views.

    Any assistance would be grateful.

    Thanks Dibs

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Apr 19, 2016 @ 18:38
    Dan Diplo
    100

    Hi,

    You just need to add

    @using Umbraco.Core.Logging
    

    to the top of your view (after the @inherits statement) and then you can call it like you do in your controller. The @using statement imports a namespace.

    Complete example:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Umbraco.Core.Logging
    @{
        Layout = "Master.cshtml";
    }
    
    <h1>Some HTML etc</h1>
    
    @{ 
        try
        {
            // do something
        }
        catch (Exception ex)
        {
            LogHelper.Error(this.GetType(), "Your message", ex);
        }
    }
    
    <p>Some more HTML</p>
    
  • Steven Harland 78 posts 518 karma points c-trib
    Apr 19, 2016 @ 19:18
    Steven Harland
    0

    Hi Dibs,

    Note that while Dan's example will work for logging from your views you won't be able to catch syntax errors like this. Syntax errors will cause an error at compile time so the code in your view won't even get executed when they happen.

    Umbraco already writes these errors to the trace log though so you should be able to see them.

  • Dibs 202 posts 991 karma points
    Apr 27, 2016 @ 13:37
    Dibs
    0

    Cheers Steven/Dan

    Thanks for the advice

    Cheers Dibs

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies