<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ertekin Özturgut</title>
    <description>The latest articles on Forem by Ertekin Özturgut (@ertekinozturgut).</description>
    <link>https://forem.com/ertekinozturgut</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F976352%2F8e258456-cafc-4007-8365-2a2d266be2dc.jpeg</url>
      <title>Forem: Ertekin Özturgut</title>
      <link>https://forem.com/ertekinozturgut</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ertekinozturgut"/>
    <language>en</language>
    <item>
      <title>How to Set Up Email Alerts for Schedule Task Errors in .NET</title>
      <dc:creator>Ertekin Özturgut</dc:creator>
      <pubDate>Sat, 07 Sep 2024 15:22:17 +0000</pubDate>
      <link>https://forem.com/ertekinozturgut/how-to-set-up-email-alerts-for-schedule-task-errors-in-net-2ebj</link>
      <guid>https://forem.com/ertekinozturgut/how-to-set-up-email-alerts-for-schedule-task-errors-in-net-2ebj</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Tracking Task Schedule errors in Windows Servers used to be easier. We could select Task Schedule logs through the Event Viewer with just a few clicks. Then, based on the filters we applied, we could send an email to the relevant people. Unfortunately, this feature was removed quite a while ago. However, we are still fortunate to have two important tools. First, triggering alerts based on conditions after a new event log entry. Second, querying event log history with C#.&lt;/p&gt;

&lt;p&gt;In this article, we will focus on the second option, examining Task Schedule errors with C# and setting up an email notification system for critical issues. At the end of the article, you will find the sample project I created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffsmwhdtzgpt06izrg6is.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffsmwhdtzgpt06izrg6is.gif" alt="Lets Do This" width="480" height="400"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Creating Project
&lt;/h2&gt;

&lt;p&gt;We will create a simple .NET Framework ConsoleApp. The primary reason for not choosing .NET Core or .NET 5+ is to ensure that our application can run on older Windows Servers without the need for any framework installations or additional configurations. I will skip the project creation steps.&lt;/p&gt;




&lt;h2&gt;
  
  
  AppConfig Settings
&lt;/h2&gt;

&lt;p&gt;First, we need to define the necessary settings in the appconfig file. If, like me, you plan to use this application on different servers, you can make the application settings more dynamic and easier to manage by configuring them this way. To create the App.Config in the project, right-click on the ConsoleApp, select New Item, and create your config file as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1f04exjmevz9conqm6pa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1f04exjmevz9conqm6pa.png" alt="Image description" width="800" height="554"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the file is created, update it as follows. You will need to adapt this configuration based on your own environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8" ?&amp;gt;
&amp;lt;configuration&amp;gt;
  &amp;lt;startup&amp;gt;
    &amp;lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" /&amp;gt;
  &amp;lt;/startup&amp;gt;
  &amp;lt;appSettings&amp;gt;
    &amp;lt;add key="FromEmail" value="{{From Email}}" /&amp;gt;
    &amp;lt;add key="ToEmails" value="{{To Emails}}" /&amp;gt;
    &amp;lt;add key="SmtpServer" value="{{Smtp Server}}" /&amp;gt;
    &amp;lt;add key="SmtpPort" value="{{Smtp port}}" /&amp;gt;
    &amp;lt;add key="SmtpUser" value="{{Smtp user name}}" /&amp;gt;
    &amp;lt;add key="SmtpPassword" value="{{Smtp password}}" /&amp;gt;
    &amp;lt;add key="EnableSsl" value="true" /&amp;gt;
    &amp;lt;add key="TrackingServerName" value="{{The server name of you want to monitoring}}" /&amp;gt;
    &amp;lt;add key="EventViewSource" value="Microsoft-Windows-TaskScheduler/Operational" /&amp;gt;
  &amp;lt;/appSettings&amp;gt;
&amp;lt;/configuration&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The most critical variable in the code above is the EventViewSource. The value we defined here, Microsoft-Windows-TaskScheduler/Operational, contains the history of the tasks that are being monitored in the Event Viewer.&lt;/p&gt;


&lt;h2&gt;
  
  
  Getting Task Errors From Event Viewer
&lt;/h2&gt;

&lt;p&gt;After configuring the settings, we will quickly build the structure to read the Event Viewer within our main function, as shown below. Here, I focused on Task logs of the Error type. If you want, you can explore different log types or add date and time filters for more detailed results.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;






&lt;h2&gt;
  
  
  Email Sending
&lt;/h2&gt;

&lt;p&gt;Now, we define our simple email sending structure.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I send the entire error converted to JSON format in the email. If you prefer, you can include only the variables you find necessary in the email, or you could convert the JSON to an HTML table as an option.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;If you're not using a scheduling system like Hangfire, installing a separate application for each task, and looking for a simple solution to track these events rather than relying on specific tools, this method should help you.&lt;/p&gt;

&lt;p&gt;You can review the project &lt;a href="https://github.com/ertekinozturgut/TaskScheduleErrorTracker" rel="noopener noreferrer"&gt;on GitHub by clicking here.&lt;/a&gt;  inceleyebilirsiniz &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;See you again at new article. Have a nice day.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F62fdv1ymdkyggp2ty3f8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F62fdv1ymdkyggp2ty3f8.gif" alt="May the force with you" width="500" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>monitoring</category>
      <category>microsoft</category>
      <category>taskscheduler</category>
    </item>
  </channel>
</rss>
