Wednesday, April 14, 2010

c# System Event logging

Here is a simple c# method to write errors to the event log which captures the calling class and method and an error and writes them to the event log for centralized error reporting.

I don't know about you but I hate to have to track down where someone writes error messages too, so I of course encourage my employees to write any issues to an event log.

This method returns false if it fails to write to the event log.

public static bool Log_writeError(string sMessage)
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();

EventLog myLog = new EventLog();
myLog.Source = methodBase.ReflectedType.FullName + "." + methodBase.Name;
myLog.Log = "Application";

try
{
myLog.WriteEntry(sMessage, EventLogEntryType.Error);
}
catch
{
return false;
}

return true;
}

And now we make the call:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.logentry();
Console.ReadKey();
}

public void logentry()
{
if (!logger.Log_writeError("Test Message"))
Console.WriteLine("Failed to write to event log");
}
}

and we can now see an entry for our Source in the Application log:



Saturday, April 10, 2010

Deploying a C# WCF project as an installable service:

1) Create a new WCF Service Library

2) Add a new Windows Service project to the solution

a. Right click on Service1.cs and select view code and modify the OnStart and OnStop to startup and stop your WCF

b. Double click on Service1.cs

a. Right click in the window and select “Add Installer”

b. You should now see serviceinstall1 and serviceprocessinstaller1

Check the properties under these two and modify as required.

3) Add a new project “Setup Project” to your solution.

a. Right click on the project and select Add and then Project Output

b. Select your Windows service project and select Primary Output and hit OK

c. Right click on the project and select View and then Custom Actions

d. Right click on custom actions and select Add Custom Action

Double click on Application Folder and select your primary output and hit ok

After you build the “Setup Project” you will be able to run the “msi” file to install your service anywhere.

Friday, April 9, 2010

Steps for SSIS impersonation in SSMS

1) (Security -> Logins) Have a login with appropriate permissions

2) (Security -> Credentials) Create a credential and associate it with a Domain account

3) (SQL Server Agent -> Proxies) Create a new proxy and associate it with the new Credential

Under “Activate the following subsystems”

a) Select “Operating System”

b) Select “SSIS package”

4) (Security -> Logins) Select properties of our user and check “Map to credential” and add our credential

5) (SQL Server Agent -> Jobs) Select properties of your job then select steps and then edit and under Run As select our new credential

Impersonating the current Windows user


SQL server 2008 seems to make life much easier with integrated security so that us DBA s no longer have to manage passwords. All we have to do is setup what AD groups have which permissions on whatever tables.


I use the following code when accessing a SQL server database with Integrated security so that I can control user permissions through AD groups and accounts based on what credentials are currently in use.


using (((WindowsIdentity)User.Identity).Impersonate()) {

// SQL statements

}