Thursday, March 11, 2010

How to Write to the Event Log using C#

 /// 
    ///  Logs a message to the eventlog and returns true if successful.
    /// 
    public static bool LogMessageToEventLog(string source, string logName, string message, EventLogEntryType eventType, int eventId)
    {
        //need to impersonate user in order to gain access to the EventLog.SourceExists method
        System.Security.Principal.WindowsImpersonationContext impersonationContext=null;
        try
        {
            //impersonate the current windows user -- assumes your application is using windows authentication
            impersonationContext = ((System.Security.Principal.WindowsIdentity)HttpContext.Current.User.Identity).Impersonate();



            //First we need to make sure that this source doesnt exist yet
            if (!EventLog.SourceExists(source))
            {
                //Create our source by providing the source and the name of the
                //new log name
                EventLog.CreateEventSource(source, logName);

                //Due to latency we want to wait a bit for the source and
                //log to be created. So we will sleep for 3 seconds
                System.Threading.Thread.Sleep(3000);
            }

            //Now we need a new instance of the EventLog
            EventLog log = new EventLog();
            //Set the source we're writing to
            log.Source = source;

            //Now write an entry
            log.WriteEntry(message, eventType, eventId);
            Debug.Print(message);
            return true;
        }
        catch (Exception ex)
        {
            Debug.Print(ex.Message);
            return false;
        }
        finally {
            if (impersonationContext != null) impersonationContext.Undo();
        }


    }

Saturday, February 27, 2010

Monitoring the Event Log

As per a book entitled .NET Web Services by Joseph Bustos and Karli Watson, the following are good enterprise monitoring software to monitor Event Log entries:


Friday, January 29, 2010

Formatting as Currency

If you need to format a number as currency, do this:

declare @d money set @d=50000.52
select '$' + convert(varchar(50), @d,1)

Result: $50,000.52

Though it should be noted that usually, formatting is a front-end task, and should not to be relegated to the database as a matter of best practice.

Wednesday, January 06, 2010

How to Install Windows Service

This article was very helpful in showing how to create an installer file for a widows service application.

Monday, November 09, 2009

SyntaxHighlighter

SyntaxHighlighter is a fully functional self-contained code syntax highlighter

This is an example usage of syntax highlighting:

This is a block of SQL:
 --this is a comment
 Select * from tableName where Iam='you'
 and my='crazy' and theyAre=false
 and thisIs like 'this or that%'
 
This is a block of C# code:
 //This is a commment... just a comment
 private void DoSomething()
 {
  return null;
 }
 

Here is a simple example which will work so long as you don't have complex html (in that case see the documentation):

    
     //This is a commment... just a comment
     private void DoSomething()
     {
      return null;
     }