Links : Part 1, Part 2, Part 4
Creating Event Provider Assembly and using it in your application.
In this article I will show you how to generate .NET c# provider code using MC.exe and compile the generated code into an assembly so that you can easily use that assembly in any of your application to write the events which you have defined in the manifest file.
Follow these steps to generate the provider code in .NET and to compile the code into a .NET assembly.
1. Generate .NET c# code with the help of the manifest xml file we have already created. Generated c# code is completely type safe and you do not need to worry about all the constants their values and where to use all those in the code. All this is handled by MC.exe. Use the following command in visual studio 2010 to generate the provider code in c# language.
MC.exe -h <target location> <namespace name for generated class> <manifest xml file path>
MC.exe -h c:\ProviderFiles CustomProvider c:\ProviderFiles\EventProviderManifest.xml
You will see the generated c# class file in your target location. Something similar to the following.
2. Generate an assembly by compiling the generated C# class file. Use the following in visual studio 2010 command prompt.
Csc.exe /out:c:\ProviderFiles\CustomProvider.dll /unsafe /t:library c:\ProviderFiles\EventProviderManifest.cs
You will see the generated assembly file in your target location. Something similar to the following.
Congratulation!
You have just created a custom windows event provider assembly which can be loaded in any of your .NET application to publish all your events defined in the instrumentation manifest file.
Let’s use the provider assembly we have just created in a .NET application to publish the events. I am creating a simple console based application which will publish person eat event. Code of console application is present in MyApplicationUsingMyProvider.cs file. Following is the code snippet for console application.
using System;
namespace MyApplicationUsingMyProvider
{
class Program
{
static void Main(string[] args)
{
CustomProvider.PROVIDER_GUID provider = new CustomProvider.PROVIDER_GUID();
provider.EventWritePERSON_EAT_EVENT("Arun", "Malik");
Console.WriteLine("Event published successfully.");
Console.WriteLine("Press any to exit...");
Console.ReadLine();
}
}
}
Note that how the MC.exe has used the symbol value from <Provider/> element from manifest xml to generate the class name. Refer Part 2
Let’s compile the MyApplicationUsingMyProvider.cs into a console application and run it. Use following commands in visual studio 2010 command prompt.
Csc.exe /out:<target exe path> /r:<path of custom provider dll> /t:exe <console app cs file path>
csc.exe /out:c:\ProviderFiles\MyApplicationUsingMyProvider.exe /r:c:\ProviderFiles\CustomProvider.dll /t:exe c:\ProviderFiles\MyApplicationUsingMyProvider.cs
You will see the generated console application in your target location. Something similar to following.
So, we have successfully created a custom provider and used it in a simple console based application. If you run that application and try to see the published events in either windows event viewer or any of the consumer application, you will be disappointed. Reason is that there is no link has been established yet between the consumer applications and your provider. This link is Trace session and we need to create one which will listen for the published events from our custom provider. In the Part 4 of this series I will show you how to use one of the GUI based controller application to create and configure Trace session which will listen and collect the published events from your provider.
No comments:
Post a Comment