Created
July 6, 2025 07:07
-
-
Save opentechnologist/630f3942a21d57aa242dc1049792695a to your computer and use it in GitHub Desktop.
a sample C# project that logs timestamped messages to a text file using a custom text logger class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
namespace Logger | |
{ | |
public class TextLoggerService | |
{ | |
private readonly string filePath; | |
public TextLoggerService() | |
{ | |
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); | |
string fileName = string.Format("{0}.log", DateTime.Now.ToString("yyyyMMdd")); | |
filePath = Path.Combine(desktopPath, fileName); | |
if (!File.Exists(filePath)) | |
{ | |
using (FileStream fs = File.Create(filePath)) | |
{ | |
fs.Close(); | |
} | |
} | |
} | |
public void LogText(string message) | |
{ | |
string logLine = string.Format("[{0}] {1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), message, Environment.NewLine); | |
using (StreamWriter writer = new StreamWriter(filePath, true)) | |
{ | |
writer.Write(logLine); | |
writer.Flush(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading; | |
using Logger; | |
namespace MainPackage | |
{ | |
public class MainClass | |
{ | |
public static void Main(string[] args) | |
{ | |
TextLoggerService logger = new TextLoggerService(); | |
Random random = new Random(); | |
List<string> messages = new List<string> | |
{ | |
"Application Started.", | |
"Performing Action 1.", | |
"Performing Action 2.", | |
"Performing Action 3.", | |
"Performing Action 4.", | |
"Performing Action 5.", | |
"Saving Collected Results.", | |
"Application Ended." | |
}; | |
foreach (string message in messages) | |
{ | |
Console.WriteLine(message); | |
logger.LogText(message); | |
Thread.Sleep(random.Next(500, 3000)); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<Project | |
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" | |
Sdk="Microsoft.NET.Sdk" | |
DefaultTargets="Build" | |
> | |
<PropertyGroup> | |
<AssemblyName>main</AssemblyName> | |
<OutputPath>bin\</OutputPath> | |
</PropertyGroup> | |
<ItemGroup> | |
<Compile Include="main.cs" /> | |
<Compile Include="Logger.cs" /> | |
</ItemGroup> | |
<Target Name="Build" Inputs="@(Compile)" Outputs="$(OutputPath)$(AssemblyName).exe"> | |
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" /> | |
<Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe"> | |
<Output TaskParameter="OutputAssembly" ItemName="OutputFile" /> | |
</Csc> | |
<Message Text="Generated file: @(OutputFile)" Condition="Exists('@(OutputFile)')" /> | |
</Target> | |
<Target Name="Clean" > | |
<Delete Files="$(OutputPath)$(AssemblyName).exe" /> | |
</Target> | |
<Target Name="Rebuild" DependsOnTargets="Clean;Build" /> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment