Quick Application Logger Code

//todo: check anonymous (elevated privileges?)
public class Log
{
     private const string LogSource = "mYsOURCE";
     public static void WriteEntry(Exception Event)
     {
           string sSource = LogSource;
           string sLog = "Application";
           SPSecurity.RunWithElevatedPrivileges(() =>
           {
                 if (!EventLog.SourceExists(sSource))
                      EventLog.CreateEventSource(sSource, sLog);
                 try
                 {
                      EventLog.WriteEntry(sSource, Event.ToString());
                 }
                 catch { }
           });
     }


     public static void WriteEntry(string msg)
     {          

           string sSource = LogSource;
           string sLog = "Application";
           SPSecurity.RunWithElevatedPrivileges(() =>
           {                

                 if (!EventLog.SourceExists(sSource))
                      EventLog.CreateEventSource(sSource, sLog);
                 try
                 {                    

                      EventLog.WriteEntry(sSource, msg);
                 }                

                 catch { }
           });
     }
 
    

    
     public static void WriteErrorEntry(Exception Event)
     {
 
 
         string sSource = LogSource;
           string sLog = "Application";
           SPSecurity.RunWithElevatedPrivileges(() =>
           {
 
 
               if (!EventLog.SourceExists(sSource))
                      EventLog.CreateEventSource(sSource, sLog);
                 try
                 {
                      EventLog.WriteEntry(sSource, Event.ToString(), EventLogEntryType.Error);
 
 
              }
 
 
               catch { }
           });
     }
 
 


     public static void WriteErrorEntry(string msg)
     {
 
 
         string sSource = LogSource;
           string sLog = "Application";
           SPSecurity.RunWithElevatedPrivileges(() =>
           {
 
 
               if (!EventLog.SourceExists(sSource))
                      EventLog.CreateEventSource(sSource, sLog);
                 try
                 {
                      EventLog.WriteEntry(sSource, msg, EventLogEntryType.Error);
 
 
              }
 
 
               catch { }
           });
     }
 
 

}
 

Comments

Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem

Asp.Net Ending Response options, Response.End() vs CompleteRequest()