Skip to content

Instantly share code, notes, and snippets.

@opentechnologist
Created June 25, 2025 05:22
Show Gist options
  • Save opentechnologist/40b2211562314f5a66bc196538ce0f4b to your computer and use it in GitHub Desktop.
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.
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