Altijd als eerste onze nieuwste blogs lezen? Laat je email adres achter en je ontvangt een bericht als wij een nieuw blog plaatsen.

When consulting customers that already have running websites on Sitecore. Most time I get the question : “Jeroen can you look at the following message from our Sitecore log file.

Governance model

Onze technische blogs zijn in het Engels. Dit doen wij omdat wij menen dat technische kennis van Sitecore en Sitefinity grensoverschrijdend moet zijn. Wij leren veel van buitenlandse developers, wij delen onze kennis ook graag met hen. Mochten er in de code fouten zitten of punten ter verbetering, dan gaan we graag het gesprek aan.

When consulting customers that already have running websites on Sitecore. Most time I get the question: “Jeroen can you look at the following message from our Sitecore log file. Our technical application administrator does not know what to do with it, and how it should be resolved. We want these messages out of our log files”. 

It is important to say that most customers are hosting multiple websites within a single Sitecore Instance sometimes build by different Sitecore partners. 

Below are examples of messages that I often see in log files;

5388 09:13:46 WARN ID = null 
Or 
5388 09:13:46 ERROR ‘object not set to an instance of an object’ 

During building of the website the developers thought to write to a log file, but not with maintenance in mind. With those messages above you cannot find or solved the problem. You know there is something’s wrong and the only thing you can do is to wait when website visitors will be reporting errors. Hopefully with that information you can find the problem and resolve it. 

When I am writing to a log file. I have a rule that you always have to specify which class, method, namespace, especially which site it concerned. So you can see where the message comes from and where to look in the code. 

Now I'm always a bit lazy and thought can you do this easier? 

And the answer is Yes, I think so. 

You can use the StackTrace class to retrieve a collection of multiple Stackframe classes. It is then important to find out which stackframe you should use. 

I have written a separate log wrapper class that writes the final message via the Sitecore log class. My wrapper only ensures therefore that the extra information is automatically added to the message log. 

If you use this approach then it makes no sense to get the first Stackframe from the StackTrace class, since this is always will be the call on the method of the Log Wrapper. In my case I have to get the second stackframe, this is where the log message is created and where you must be in the code. 

Below is a piece of demo code of logging wrapper class. When you want to use it in production you have to make some code improvements. But for this blog I think it’s all clear what the intention is

 
       public static class Logging
    {
 
        public static void Debug(string message, object owner)
        {
            Write2Log(message, owner, "debug");
        }
 
        public static void Info(string message, object owner)
        {
            Write2Log(message, owner, "info");
        }
 
        public static void Warning(string message, object owner)
        {
            Write2Log(message, owner, "warning");
        }
 
        public static void Error(string message, object owner)
        {
            Write2Log(message, owner, "error");
        }
 
        public static void Fatal(string message, object owner)
        {
            Write2Log(message, owner, "fatal");
        }
 
        private static void Write2Log(string message, object owner, string type)
        {         
            const string format = "[{0}] [{1}] : {2}";
 
            var stack = new StackTrace(new StackFrame(1, true));
            int start = stack.ToString().IndexOf("at ", StringComparison.Ordinal);
            int end = stack.ToString().IndexOf(" in ", StringComparison.Ordinal);
            string method = (end > 4) ? stack.ToString().Substring(start + 2, end - 4) : stack.ToString().Substring(start + 2);
            string sitename = Sitecore.Context.Site.Name;
 
            string msg = string.Format(CultureInfo.InvariantCulture, format, sitename, method, message);
 
            switch(type.ToLowerInvariant())
            {
                case  "info":
                    Sitecore.Diagnostics.Log.Info(msg, owner);
                    break;
                case "warning":
                    Sitecore.Diagnostics.Log.Warn(msg, owner);
 
                    break;
                case "error":
                    Sitecore.Diagnostics.Log.Error(msg, owner);
                    break;
                case "fatal":
                    Sitecore.Diagnostics.Log.Fatal(msg, owner);
                    break;
            }
 
 
        }
Conclusion 

It’s important when writing to the Sitecore log file, that you not only write the message. But also write the Sitename, namespace and method name. So when the message occur in the log file, it won’t be a puzzle to find out where the message is coming from. In this blog I made a start on how you can automated this task.

Meer achtergrond nodig? Neem contact met ons op

Deel deze pagina