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

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

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...

Create Document Dynamically (Umbraco)

You can create document and publish the content during run time, Below code snippets helps you to do it. public class UmbracoUtils { /// <summary> /// Create and publish the document programatically /// </summary> /// <param name="nodeName"></param> /// <param name="properties"></param> /// <param name="documentType"></param> /// <param name="parentId"></param> /// <returns>node id</returns> public static int CreateDocument(string nodeName, Dictionary<string, string> properties, int documentType, int parentId, List<string> roles = null) { DocumentType dt = new DocumentType(documentType); umbraco.BusinessLogic.User u = new umbraco.BusinessLogic.User(0); // Create the document Document d = Document.MakeNew(nodeName, dt, u, parentId); // Add values...