Skip to main content

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);  
         }  
       }  
     }  

Comments

Popular posts from this blog

Beyond Solo Assistants: Google's Vision for AI Teamwork (Agent-to-Agent Collaboration)

We talk a lot about AI assistants like Google Assistant or chatbots answering our questions. They're pretty smart on their own, right? But imagine if they could team up, combine their unique skills, and tackle really complex problems together, just like a human team does. That's the core idea behind a super exciting area Google and others in the AI world are exploring: Agent-to-Agent (A2A) communication and collaboration. Think of it less as a single product called "Agent2Agent" and more as the science and engineering of building AI teams. Ready to explore why this is such a big deal? Let's break it down! First Off: What Even is an AI Agent? Think of an AI agent as a specialized digital helper. It's a piece of software designed to: Perceive: Understand its environment (text, images, data, user requests). Reason: Figure out the best course of action based on its goals and knowledge. Act: Perform tasks (answer questions, writ...

Why not to have a static const in c#

This is just a thought, that I was thinking why can't we have a constant with static in C#, and the answer is 'NO'; That we cannot have a static constant; e.g: I created a class as below: public class Constants1 { public const string Const1 = "Hello"; public const string Const2 = "World"; public static string Static1 = "Hello Static"; } When we compile the program into IL, the C# compiler does a magic in IL, that the constants converts into static literals, of course it has to, that's why we are able to access the constants as Constants1.Const1

DD4T: Customizing RenderComponentPresentation

Sometime when we work with Component presentation, we might come up with the scenario to have customized RenderComponentPresentation, when we deal with Area/View or splitting view into more organized structure. We can fetch and send the area from the Template. Currently DD4T doesn’t allow injecting the ComponentPresentationRenderer, instead it allows us to write our own renderer and call it along with RenderComponentPresentation as below The custom ComponentPresenationRenderer can be created based on the scenario, so when we use the area, we could have the custom ComponentPresentation as below SDL Reference implementation, customized Renderer beautifully and implemented the Renderor to use Area view concept.