Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4610846
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:11:18+00:00 2026-05-22T01:11:18+00:00

I am developing the Windows Service using C# and .Net framework 4.0. In order

  • 0

I am developing the Windows Service using C# and .Net framework 4.0.

In order for the Windows Service to write the entry to the EventLog, I need to check and create the Event Log Source if it is not existed.

if (EventLog.SourceExists("MySource") == false)
            EventLog.CreateEventSource("MySource", "MyLog"); 

I am confusing about where I should check and create the Event Log Source. Should I create in the Constructor method of the Windows Service or Constructor method of the Installer Service?

When I googled, I found both ways. So, could you please advise the best practice for me? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:11:19+00:00Added an answer on May 22, 2026 at 1:11 am

    I think it would be better to add the Event Log Source in the installer, but I would also check to make sure that Event Log Source exists when the service starts up. Also, rather than adding the Event Log Source in the constructor, you probably want to use one of the installer events. I haven’t done this before, but I think the AfterInstall event looks like a good place for adding Event Log Sources (see the answer to this question for more info: Automatically start a Windows Service on install).

    The reason for using the installer service to add the Event Log Source is because (as @MusiGenesis pointed out) you’re more likely to have the security access you need to create the Event Log Source when the application is being installed. When the service is running, it could be set to an account that does not have permissions to create Event Log Sources.

    In the service itself, I would check to make sure the Event Log Source exists as well. If it is not present you could try to recreate it, and if that attempt fails then you could take whatever action best suits your needs. For example:

    • Log an error message to the file
      system somewhere stating that logging
      is for the service is disabled
    • Throw an exception so the service doesn’t
      start (which should cause Windows to
      write Event Log entries of its own)
    • Disable logging entirely, etc.

    If you check (and try to create the Event Log Source if it does not exist) in the service then you get the added minor benefit of the service being able to tolerate a user or other program deleting the Event Log Source your service is relying on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a Windows Mobile 5.0 or above WinForm application using .NET Compact Framework
I'm developing a service using .NET on Windows platforms. It had worked until yesterday...
I am developing a Windows .NET application (WinForms) and I need to simulate a
I am developing a web application with Azure Cloude Service and using ASP.NET 3.5,
I'm developing a windows service in C# .Net and i'm having some dificulty to
I'm developing and application that runs as a Windows service. There are other components
I am developing a Windows service that exposes a WCF service intended to be
I have a project ongoing at the moment which is create a Windows Service
We're developing a windows service which will act as a sort of 'slave' process.
As I am working on .Net windows form application, we're using MongoDB for back-end

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.