Created
June 25, 2025 05:22
-
-
Save opentechnologist/40b2211562314f5a66bc196538ce0f4b to your computer and use it in GitHub Desktop.
a custom text logger in C# that will log text to a file created on the user's desktop.
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; | |
public class CustomTextLogger<T> | |
{ | |
private readonly string filePath; | |
public CustomTextLogger() | |
{ | |
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); | |
string fileName = $"{DateTime.Now:yyyyMMdd}.log"; | |
filePath = Path.Combine(desktopPath, fileName); | |
if (!File.Exists(filePath)) | |
using (File.Create(filePath)) { } | |
} | |
public void logText(string content) | |
{ | |
string logLine = $"{DateTime.Now:[yyyy-MM-dd HH:mm:ss]} {content}{Environment.NewLine}"; | |
using (var writer = new StreamWriter(filePath, true)) | |
{ | |
writer.Write(logLine); | |
writer.Flush(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment