27 November, 2011

Logger with UmbracoLog

Umbraco logger is powerful tool and It can be extended to use in your Umbraco application to log anything that you want to do, instead of using another logger to do the task.
     /// <summary>  
     /// Logger to log Error/Debug/Warning to Umbraco log table  
     /// </summary>  
     public class Logger  
     {  
       public enum LogLevels  
       {  
         DEBUG = 1,  
         WARNING = 2,  
         ERROR = 3,  
         NONE = 100  
       }  
       public static LogLevels CurrentLevel  
       {  
         get;  
         set;  
       }  
       private static string StrLoggedInUserId;  
       public static void SetStrLoggedInUserId()  
       {  
         if (StrLoggedInUserId == null || StrLoggedInUserId == "NA")  
         {  
           if (HttpContext.Current != null)  
           {  
             HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];  
             if (cookie != null)  
             {  
               FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);  
               StrLoggedInUserId = ticket.Name;  
               return;  
             }  
           }  
           if (string.IsNullOrEmpty(StrLoggedInUserId))  
           {  
             StrLoggedInUserId = "NA";  
           }  
         }  
       }  
       private static string StrSessionId  
       {  
         get  
         {  
           return HttpContext.Current.Session != null ? HttpContext.Current.Session.SessionID : "NA";  
         }  
       }   
       public static void Debug(string message)  
       {  
         SetStrLoggedInUserId();  
         if ((int)CurrentLevel <= (int)LogLevels.DEBUG)  
         {  
           umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, -1, StrLoggedInUserId + " : " + StrSessionId + " : " + message);  
         }  
       }  
       public static void Error(string message)  
       {  
         SetStrLoggedInUserId();  
         if ((int)CurrentLevel <= (int)LogLevels.ERROR)  
         {  
           umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error, -1, StrLoggedInUserId + " : " + StrSessionId + " : " + message);  
         }  
       }  
       public static void Exception(Exception e)  
       {  
         SetStrLoggedInUserId();  
         if ((int)CurrentLevel <= (int)LogLevels.ERROR)  
         {  
           string logstring = e.Message;  
           logstring += Environment.NewLine;  
           logstring += e.StackTrace != null ? e.StackTrace : "";  
           logstring += Environment.NewLine;  
           logstring += e.InnerException != null ? e.InnerException.Message : "";  
           logstring += Environment.NewLine;  
           logstring += e.InnerException != null && e.InnerException.StackTrace != null ? e.InnerException.StackTrace : "";  
           umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Custom, -1, StrLoggedInUserId + " : " + StrSessionId + " : " + logstring);  
         }  
       }  
       public static void Login(string message)  
       {  
         SetStrLoggedInUserId();  
         if ((int)CurrentLevel <= (int)LogLevels.DEBUG)  
         {  
           umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Login, -1, StrLoggedInUserId + "~" + StrSessionId + "~" + message);  
         }  
       }  
       public static void ErrorLogin(string message)  
       {  
         SetStrLoggedInUserId();  
         if ((int)CurrentLevel <= (int)LogLevels.ERROR)  
         {  
           umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.LoginFailure, -1, StrLoggedInUserId + "~" + StrSessionId + "~" + message);  
         }  
       }  
       public static void Logout(string message)  
       {  
         SetStrLoggedInUserId();  
         if ((int)CurrentLevel <= (int)LogLevels.DEBUG)  
         {  
           umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Logout, -1, StrLoggedInUserId + "~" + StrSessionId + "~" + message);  
         }  
       }  
     }  

0 comments:

Post a Comment