Skip to content

Commit 2fbf9cd

Browse files
committed
Alter App.Config With The New Informations
Create Global Class For Global Members Create Log Class For File Methods (LogMessages - Create Directories)
1 parent 388686e commit 2fbf9cd

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

App.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
</startup>
66
<appSettings>
77
<add key="ConnectionString" value="Server=.;Database=MySchool;Integrated Security=True;" />
8-
<add key="BackupFolder" value="C:\DatabaseBackups" />
9-
<add key="LogFolder" value="C:\DatabaseBackups\Logs" />
8+
<add key="BackupFolder" value="C:\MyServices\MyBackupService\DatabaseBackups" />
9+
<add key="LogFolder" value="C:\MyServices\MyBackupService\DatabaseBackups\Logs" />
1010
<add key="BackupIntervalMinutes" value="60" />
1111
</appSettings>
1212
</configuration>

MyDatabaseBackupService.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
</PropertyGroup>
3535
<ItemGroup>
3636
<Reference Include="System" />
37+
<Reference Include="System.Configuration" />
38+
<Reference Include="System.Configuration.Install" />
3739
<Reference Include="System.Core" />
3840
<Reference Include="System.Xml.Linq" />
3941
<Reference Include="System.Data.DataSetExtensions" />
@@ -44,6 +46,8 @@
4446
<Reference Include="System.Xml" />
4547
</ItemGroup>
4648
<ItemGroup>
49+
<Compile Include="clsGlobal.cs" />
50+
<Compile Include="clsLog.cs" />
4751
<Compile Include="MyBackupService.cs">
4852
<SubType>Component</SubType>
4953
</Compile>

clsGlobal.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Configuration;
2+
using System.IO;
3+
4+
namespace MyDatabaseBackupService
5+
{
6+
internal class clsGlobal
7+
{
8+
public static string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
9+
public static string BackupFolder = ConfigurationManager.AppSettings["BackupFolder"];
10+
public static string logFolder = ConfigurationManager.AppSettings["LogFolder"];
11+
public static string logFilePath = Path.Combine(logFolder, "ServiceLog.txt");
12+
13+
}
14+
}

clsLog.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace MyDatabaseBackupService
5+
{
6+
internal class clsLog
7+
{
8+
// Log a message to a file with a timestamp
9+
//The service logs all its state transitions (Start, Stop, Pause, Continue, Shutdown) to a file named ServiceStateLog.txt in the configured directory.
10+
// Each log entry includes a timestamp for tracking purposes.
11+
public static void LogServiceEvent(string message)
12+
{
13+
string logMessage = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}\n";
14+
File.AppendAllText(clsGlobal.logFilePath, logMessage);
15+
16+
// Write to console if running interactively
17+
if (Environment.UserInteractive)
18+
{
19+
Console.WriteLine(logMessage);
20+
}
21+
}
22+
23+
public static bool CreateFolderIfDoesNotExist(string FolderPath)
24+
{
25+
26+
// Check if the folder exists
27+
if (!Directory.Exists(FolderPath))
28+
{
29+
try
30+
{
31+
// If it doesn't exist, create the folder
32+
Directory.CreateDirectory(FolderPath);
33+
return true;
34+
}
35+
catch (Exception ex)
36+
{
37+
return false;
38+
}
39+
}
40+
41+
return true;
42+
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)