Copied to clipboard

Flag this post as spam?

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


  • Paul Blair 466 posts 731 karma points
    Feb 14, 2013 @ 04:59
    Paul Blair
    2

    What is the correct best way for logging custom errors

    Hi,

    In the past I would do something like:

    umbraco.BusinessLogic.Log.Add(...)

    which is not getting reported as obsolete and that I should use LogHelper

    So I now have

    LogHelper.Error<????>(errorMessage, exception)

    What should I be using as the type? And is this the recomended way for logging errors?

    Cheers

    Paul

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 14, 2013 @ 13:05
    Jeroen Breuer
    0

    Maybe check the Umbraco source to see how the LogHelper is used there?

    Jeroen

  • Joao Pinto 24 posts 64 karma points
    Mar 01, 2013 @ 14:47
    Joao Pinto
    11

    "The friendliest CMS community on the planet" :)

    Umbraco logging uses log4net, the TYPE should be the type of the object / class you are loggin, usually is used in this way:

    //You can specificy the class your are logging
    LogHelper.Error(typeof(MyClass), "msg", e);
    //Otherwise, you can use this code, that gets type automatically
    LogHelper.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "msg", e);
  • Paul Blair 466 posts 731 karma points
    Mar 03, 2013 @ 22:00
    Paul Blair
    0

    Hi Joao,

    Thanks for your help. I'm doing my logging within a Razor script so I was unsure what class was being logged. The full script I am testing is:

    try
        {
            client.EnableSsl = true;
            client.Send(mail);
            <p><strong>Your contact request has been sent. Someone will be in contact shortly</strong></p>
        }
        catch (Exception ex)
        {
            //LogHelper.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Error creating or sending contact mail", ex.InnerException);
            umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error, -1, string.Format("Error creating or sending contact mail: {0}", ex.InnerException));
            <p>Error: An error has occured sending your contact details. Please contact us directly using the phone or email listed above.</p>
        }

    Even the old way of doing things is not recording an entry in the umbracoLog table so I'm wondering if there is an issue with v6.

    Cheers

    Paul

  • Joao Pinto 24 posts 64 karma points
    Mar 04, 2013 @ 09:20
    Joao Pinto
    3

    Hi Paul,

    currently I haven't tried the old api for logging on Umbraco V6, but I've used the new one and it works properly.

    So I suggest you to just change your code to use the new API, this simple code will work:

    LogHelper.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "msg", ex);
  • Peter Duncanson 430 posts 1360 karma points c-trib
    May 22, 2013 @ 12:36
    Peter Duncanson
    1

    @Joao, v6 now logs using log4net so take a look in /App_data/logs/ for your log entries and not the DB table. Hope that helps.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 19, 2013 @ 10:43
    Jeavon Leopold
    3

    Really useful post thanks! I would add that to use the v6 log4net LogHelper you need to reference Umbraco.Core.Logging

    e.g in a Razor script:

    @using Umbraco.Core.Logging;
    @{
    LogHelper.Info(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Message"); 
  • Adrian 38 posts 117 karma points
    Jul 09, 2014 @ 11:40
    Adrian
    109

    Much less typing!

    @using Umbraco.Core.Logging;
    @{
        LogHelper.Info(this.GetType(), "Message"); 
    } 
    
  • Steven Harland 78 posts 518 karma points c-trib
    Jul 15, 2014 @ 10:54
    Steven Harland
    0

    Darren Ferguson has a great blog post on custom logging here.

    Maybe not exactly what you need but certainly useful nonetheless.

  • Michael Falk 37 posts 59 karma points
    Jan 26, 2015 @ 08:15
    Michael Falk
    1

    OK, answering myself, addding

    using Umbraco.Core.Logging;

    then you can call:

    LogHelper.Info(this.GetType(),string.Format("blblb"));

     UPS, this was an answer to: http://our.umbraco.org/forum/developers/api-questions/60739-What-is-the-correct-way-to-write-to-UmbracoTraceLogtxt-in-Umbraco-7

Please Sign in or register to post replies

Write your reply to:

Draft