Skip to content

Commit

Permalink
fix snippet links
Browse files Browse the repository at this point in the history
  • Loading branch information
gewarren committed Apr 26, 2024
1 parent fc75fd4 commit 851f403
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
title: "How to: Log Information About Services"
description: Know how to log information about services. Set the AutoLog property if you want your Windows Service project to interact with the Application event log.
ms.date: "03/30/2017"
dev_langs:
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
helpviewer_keywords:
- "AutoLog property"
- "services, logging information"
- "Windows Service applications, logging information about"
Expand All @@ -18,54 +18,54 @@ ms.assetid: c0d8140f-c055-4d8e-a2e0-37358a550116

[!INCLUDE [windows-service-disambiguation](../../core/extensions/includes/windows-service-disambiguation.md)]

By default, all Windows Service projects have the ability to interact with the Application event log and write information and exceptions to it. You use the <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> property to indicate whether you want this functionality in your application. By default, logging is turned on for any service you create with the Windows Service project template. You can use a static form of the <xref:System.Diagnostics.EventLog> class to write service information to a log without having to create an instance of an <xref:System.Diagnostics.EventLog> component or manually register a source.
The installer for your service automatically registers each service in your project as a valid source of events with the Application log on the computer where the service is installed, when logging is turned on. The service logs information each time the service is started, stopped, paused, resumed, installed, or uninstalled. It also logs any failures that occur. You do not need to write any code to write entries to the log when using the default behavior; the service handles this for you automatically.
If you want to write to an event log other than the Application log, you must set the <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> property to `false`, create your own custom event log within your services code, and register your service as a valid source of entries for that log. You must then write code to record entries to the log whenever an action you're interested in occurs.
By default, all Windows Service projects have the ability to interact with the Application event log and write information and exceptions to it. You use the <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> property to indicate whether you want this functionality in your application. By default, logging is turned on for any service you create with the Windows Service project template. You can use a static form of the <xref:System.Diagnostics.EventLog> class to write service information to a log without having to create an instance of an <xref:System.Diagnostics.EventLog> component or manually register a source.

The installer for your service automatically registers each service in your project as a valid source of events with the Application log on the computer where the service is installed, when logging is turned on. The service logs information each time the service is started, stopped, paused, resumed, installed, or uninstalled. It also logs any failures that occur. You do not need to write any code to write entries to the log when using the default behavior; the service handles this for you automatically.

If you want to write to an event log other than the Application log, you must set the <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> property to `false`, create your own custom event log within your services code, and register your service as a valid source of entries for that log. You must then write code to record entries to the log whenever an action you're interested in occurs.

> [!NOTE]
> If you use a custom event log and configure your service application to write to it, you must not attempt to access the event log before setting the service's <xref:System.ServiceProcess.ServiceBase.ServiceName%2A> property in your code. The event log needs this property's value to register your service as a valid source of events.
### To enable default event logging for your service
- Set the <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> property for your component to `true`.
> If you use a custom event log and configure your service application to write to it, you must not attempt to access the event log before setting the service's <xref:System.ServiceProcess.ServiceBase.ServiceName%2A> property in your code. The event log needs this property's value to register your service as a valid source of events.
## To enable default event logging for your service

- Set the <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> property for your component to `true`.

> [!NOTE]
> By default, this property is set to `true`. You do not need to set this explicitly unless you are building more complex processing, such as evaluating a condition and then setting the <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> property based on the result of that condition.
### To disable event logging for your service
- Set the <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> property for your component to `false`.
[!code-csharp[VbRadconService#17](../../../samples/snippets/csharp/VS_Snippets_VBCSharp/VbRadconService/CS/MyNewService.cs#17)]
[!code-vb[VbRadconService#17](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#17)]
### To set up logging to a custom log
1. Set the <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> property to `false`.
> By default, this property is set to `true`. You do not need to set this explicitly unless you are building more complex processing, such as evaluating a condition and then setting the <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> property based on the result of that condition.
## To disable event logging for your service

- Set the <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> property for your component to `false`.

[!code-csharp[VbRadconService#17](./snippets/MyNewService/csharp/MyNewService.cs#17)]
[!code-vb[VbRadconService#17](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#17)]

## To set up logging to a custom log

1. Set the <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> property to `false`.

> [!NOTE]
> You must set <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> to false in order to use a custom log.
2. Set up an instance of an <xref:System.Diagnostics.EventLog> component in your Windows Service application.
3. Create a custom log by calling the <xref:System.Diagnostics.EventLog.CreateEventSource%2A> method and specifying the source string and the name of the log file you want to create.
4. Set the <xref:System.Diagnostics.EventLog.Source%2A> property on the <xref:System.Diagnostics.EventLog> component instance to the source string you created in step 3.
5. Write your entries by accessing the <xref:System.Diagnostics.EventLog.WriteEntry%2A> method on the <xref:System.Diagnostics.EventLog> component instance.
The following code shows how to set up logging to a custom log.
> You must set <xref:System.ServiceProcess.ServiceBase.AutoLog%2A> to false in order to use a custom log.
2. Set up an instance of an <xref:System.Diagnostics.EventLog> component in your Windows Service application.

3. Create a custom log by calling the <xref:System.Diagnostics.EventLog.CreateEventSource%2A> method and specifying the source string and the name of the log file you want to create.

4. Set the <xref:System.Diagnostics.EventLog.Source%2A> property on the <xref:System.Diagnostics.EventLog> component instance to the source string you created in step 3.

5. Write your entries by accessing the <xref:System.Diagnostics.EventLog.WriteEntry%2A> method on the <xref:System.Diagnostics.EventLog> component instance.

The following code shows how to set up logging to a custom log.

> [!NOTE]
> In this code example, an instance of an <xref:System.Diagnostics.EventLog> component is named `eventLog1` (`EventLog1` in Visual Basic). If you created an instance with another name in step 2, change the code accordingly.
[!code-csharp[VbRadconService#14](../../../samples/snippets/csharp/VS_Snippets_VBCSharp/VbRadconService/CS/MyNewService.cs#14)]
[!code-vb[VbRadconService#14](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#14)]
[!code-csharp[VbRadconService#15](../../../samples/snippets/csharp/VS_Snippets_VBCSharp/VbRadconService/CS/MyNewService.cs#15)]
[!code-vb[VbRadconService#15](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#15)]
> In this code example, an instance of an <xref:System.Diagnostics.EventLog> component is named `eventLog1` (`EventLog1` in Visual Basic). If you created an instance with another name in step 2, change the code accordingly.
[!code-csharp[VbRadconService#14](./snippets/MyNewService/csharp/MyNewService.cs#14)]
[!code-vb[VbRadconService#14](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#14)]
[!code-csharp[VbRadconService#15](./snippets/MyNewService/csharp/MyNewService.cs#15)]
[!code-vb[VbRadconService#15](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#15)]

## See also

- [Introduction to Windows Service Applications](introduction-to-windows-service-applications.md)
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
title: "How to: Write Services Programmatically"
description: See how to write services programmatically by setting up the inheritance and other infrastructure elements yourself.
ms.date: "03/30/2017"
dev_langs:
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
helpviewer_keywords:
- "services, creating"
- "Windows Service applications, creating"
ms.assetid: 3abbb2ec-78d2-41e6-b9f9-6662d4e2cdc7
Expand All @@ -14,59 +14,59 @@ ms.assetid: 3abbb2ec-78d2-41e6-b9f9-6662d4e2cdc7

[!INCLUDE [windows-service-disambiguation](../../core/extensions/includes/windows-service-disambiguation.md)]

If you choose not to use the Windows Service project template, you can write your own services by setting up the inheritance and other infrastructure elements yourself. When you create a service programmatically, you must perform several steps that the template would otherwise handle for you:
- You must set up your service class to inherit from the <xref:System.ServiceProcess.ServiceBase> class.
- You must create a `Main` method for your service project that defines the services to run and calls the <xref:System.ServiceProcess.ServiceBase.Run%2A> method on them.
- You must override the <xref:System.ServiceProcess.ServiceBase.OnStart%2A> and <xref:System.ServiceProcess.ServiceBase.OnStop%2A> procedures and fill in any code you want them to run.
### To write a service programmatically
1. Create an empty project and create a reference to the necessary namespaces by following these steps:
1. In **Solution Explorer**, right-click the **References** node and click **Add Reference**.
2. On the **.NET Framework** tab, scroll to **System.dll** and click **Select**.
3. Scroll to **System.ServiceProcess.dll** and click **Select**.
4. Click **OK**.
2. Add a class and configure it to inherit from <xref:System.ServiceProcess.ServiceBase>:
[!code-csharp[VbRadconService#7](../../../samples/snippets/csharp/VS_Snippets_VBCSharp/VbRadconService/CS/MyNewService.cs#7)]
[!code-vb[VbRadconService#7](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#7)]
3. Add the following code to configure your service class:
[!code-csharp[VbRadconService#8](../../../samples/snippets/csharp/VS_Snippets_VBCSharp/VbRadconService/CS/MyNewService.cs#8)]
[!code-vb[VbRadconService#8](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#8)]
4. Create a `Main` method for your class, and use it to define the service your class will contain; `userService1` is the name of the class:
[!code-csharp[VbRadconService#9](../../../samples/snippets/csharp/VS_Snippets_VBCSharp/VbRadconService/CS/MyNewService.cs#9)]
[!code-vb[VbRadconService#9](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#9)]
5. Override the <xref:System.ServiceProcess.ServiceBase.OnStart%2A> method, and define any processing you want to occur when your service is started.
[!code-csharp[VbRadconService#10](../../../samples/snippets/csharp/VS_Snippets_VBCSharp/VbRadconService/CS/MyNewService.cs#10)]
[!code-vb[VbRadconService#10](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#10)]
6. Override any other methods you want to define custom processing for, and write code to determine the actions the service should take in each case.
7. Add the necessary installers for your service application. For more information, see [How to: Add Installers to Your Service Application](how-to-add-installers-to-your-service-application.md).
8. Build your project by selecting **Build Solution** from the **Build** menu.
If you choose not to use the Windows Service project template, you can write your own services by setting up the inheritance and other infrastructure elements yourself. When you create a service programmatically, you must perform several steps that the template would otherwise handle for you:

- You must set up your service class to inherit from the <xref:System.ServiceProcess.ServiceBase> class.

- You must create a `Main` method for your service project that defines the services to run and calls the <xref:System.ServiceProcess.ServiceBase.Run%2A> method on them.

- You must override the <xref:System.ServiceProcess.ServiceBase.OnStart%2A> and <xref:System.ServiceProcess.ServiceBase.OnStop%2A> procedures and fill in any code you want them to run.

### To write a service programmatically

1. Create an empty project and create a reference to the necessary namespaces by following these steps:

1. In **Solution Explorer**, right-click the **References** node and click **Add Reference**.

2. On the **.NET Framework** tab, scroll to **System.dll** and click **Select**.

3. Scroll to **System.ServiceProcess.dll** and click **Select**.

4. Click **OK**.

2. Add a class and configure it to inherit from <xref:System.ServiceProcess.ServiceBase>:

[!code-csharp[VbRadconService#7](./snippets/MyNewService/csharp/MyNewService.cs#7)]
[!code-vb[VbRadconService#7](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#7)]

3. Add the following code to configure your service class:

[!code-csharp[VbRadconService#8](./snippets/MyNewService/csharp/MyNewService.cs#8)]
[!code-vb[VbRadconService#8](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#8)]

4. Create a `Main` method for your class, and use it to define the service your class will contain; `userService1` is the name of the class:

[!code-csharp[VbRadconService#9](./snippets/MyNewService/csharp/MyNewService.cs#9)]
[!code-vb[VbRadconService#9](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#9)]

5. Override the <xref:System.ServiceProcess.ServiceBase.OnStart%2A> method, and define any processing you want to occur when your service is started.

[!code-csharp[VbRadconService#10](./snippets/MyNewService/csharp/MyNewService.cs#10)]
[!code-vb[VbRadconService#10](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#10)]

6. Override any other methods you want to define custom processing for, and write code to determine the actions the service should take in each case.

7. Add the necessary installers for your service application. For more information, see [How to: Add Installers to Your Service Application](how-to-add-installers-to-your-service-application.md).

8. Build your project by selecting **Build Solution** from the **Build** menu.

> [!NOTE]
> Do not press F5 to run your project — you cannot run a service project in this way.
9. Create a setup project and the custom actions to install your service. For an example, see [Walkthrough: Creating a Windows Service Application in the Component Designer](walkthrough-creating-a-windows-service-application-in-the-component-designer.md).
10. Install the service. For more information, see [How to: Install and Uninstall Services](how-to-install-and-uninstall-services.md).
> Do not press F5 to run your project — you cannot run a service project in this way.
9. Create a setup project and the custom actions to install your service. For an example, see [Walkthrough: Creating a Windows Service Application in the Component Designer](walkthrough-creating-a-windows-service-application-in-the-component-designer.md).

10. Install the service. For more information, see [How to: Install and Uninstall Services](how-to-install-and-uninstall-services.md).

## See also

- [Introduction to Windows Service Applications](introduction-to-windows-service-applications.md)
Expand Down
Loading

0 comments on commit 851f403

Please sign in to comment.