Mike Mengell
Mike Mengell

Reputation: 2398

How can I get the name of my Windows Service at runtime?

I have a new Windows Service I'm making in C# 4.0.

I'm trying to drop in some event logging code I wrote a while ago which is in a seperate class.

I want to set the event source to the name of the Windows Service without having to change the code next time I drop it into a service.

I'm looking for something like this:

String source = Application.Name

But I can't seem to find what I'm after anywhere.

Any takers?

Upvotes: 3

Views: 3533

Answers (2)

Simon Smeets
Simon Smeets

Reputation: 593

You will not be able to call a static property. Unless you write your own static wrapper to get the value of the ServiceName property of your ServiceBase derived class.

Upvotes: 1

Vijay Sirigiri
Vijay Sirigiri

Reputation: 4711

If the code is within the service application, you could do

String source = this.ServiceName;

Upvotes: 4

Related Questions