I'm trying to do my own Logger I have this function:
public static void LogInfo(string message)
{
if (!_settings.LogInfo) return;
var timestamp = DateTime.Now.ToString(); // Default format if DateFormat is empty
if (!string.IsNullOrEmpty(_settings.DateFormat))
{
try
{
timestamp = DateTime.Now.ToString(_settings.DateFormat);
}
catch
{
// If format is invalid, fall back to default format
timestamp = DateTime.Now.ToString();
}
}
var logEntry = new StringBuilder();
logEntry.AppendLine($"[{timestamp}] INFO: {message}");
logEntry.AppendLine("----------------------------------------");
var fileName = _settings.SeparateLogFiles ? "info" : "combined";
var logFile = Path.Combine(_settings.LogPath, $"{fileName}_{DateTime.Now:yyyy-MM-dd}.log");
WriteToLog(logFile, logEntry.ToString());
// Auto cleanup if enabled
if (_settings.AutoCleanupLogs)
{
CleanupOldLogs();
}
}
Which I call basically like this:
Logger.LogInfo("App Started")
Everythink works fine and I get message in my logfile but it looks like this:
[yyyy-MM-dd HH:mm:ss] INFO: Application started
It shows format of the timestamp instead of actual timestamp and have no idea why.
Also my settings:
Anyone have idea what could be wrong? I even don't know where it took this format: yyyy-MM-dd HH: mm: ss
Because I have different in my settings
If I'm not loading the format from my settings but set it up directly it works
I'm trying to do my own Logger I have this function:
public static void LogInfo(string message)
{
if (!_settings.LogInfo) return;
var timestamp = DateTime.Now.ToString(); // Default format if DateFormat is empty
if (!string.IsNullOrEmpty(_settings.DateFormat))
{
try
{
timestamp = DateTime.Now.ToString(_settings.DateFormat);
}
catch
{
// If format is invalid, fall back to default format
timestamp = DateTime.Now.ToString();
}
}
var logEntry = new StringBuilder();
logEntry.AppendLine($"[{timestamp}] INFO: {message}");
logEntry.AppendLine("----------------------------------------");
var fileName = _settings.SeparateLogFiles ? "info" : "combined";
var logFile = Path.Combine(_settings.LogPath, $"{fileName}_{DateTime.Now:yyyy-MM-dd}.log");
WriteToLog(logFile, logEntry.ToString());
// Auto cleanup if enabled
if (_settings.AutoCleanupLogs)
{
CleanupOldLogs();
}
}
Which I call basically like this:
Logger.LogInfo("App Started")
Everythink works fine and I get message in my logfile but it looks like this:
[yyyy-MM-dd HH:mm:ss] INFO: Application started
It shows format of the timestamp instead of actual timestamp and have no idea why.
Also my settings:
Anyone have idea what could be wrong? I even don't know where it took this format: yyyy-MM-dd HH: mm: ss
Because I have different in my settings
If I'm not loading the format from my settings but set it up directly it works
Share Improve this question edited Feb 17 at 13:58 Bill Tür stands with Ukraine 3,51530 gold badges40 silver badges51 bronze badges asked Feb 17 at 13:26 Johny WaveJohny Wave 1411 gold badge3 silver badges12 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 0Add a log statement to check whether _settings.DateFormat contains the expected value. This will help verify if the correct format is being applied. For example:
Console.WriteLine($"DateFormat: {_settings.DateFormat}");
If the output does not match the expected format, ensure that _settings.DateFormat is correctly assigned from the configuration source.
"
surrounding the format in your settings intentional? – Bill Tür stands with Ukraine Commented Feb 17 at 14:02By enclosing the entire literal string in quotation marks or apostrophes
– Matthew Watson Commented Feb 17 at 16:23