TcLog icon indicating copy to clipboard operation
TcLog copied to clipboard

NLog format ${longdate}|${level}|${logger}|${message}

Open runtimevic opened this issue 9 months ago • 8 comments

Hello

It is possible to have the NLog format??

${longdate}|${level}|${logger}|${message}

2025-05-04 15:30:25.1234|INFO|MiAplicacion.Program|Inicio de la aplicación.
2025-05-04 15:30:26.5678|DEBUG|MiAplicacion.Program|Verificando configuración de usuario.
2025-05-04 15:30:27.9876|ERROR|MiAplicacion.BaseDeDatos|Error al conectar con la base de datos.
2025-05-04 15:30:28.6543|WARN|MiAplicacion.Validaciones|Advertencia: Parámetro de entrada inválido.
2025-05-04 15:30:29.1122|INFO|MiAplicacion.Seguridad|El usuario se ha autenticado correctamente.
2025-05-04 15:30:30.2345|FATAL|MiAplicacion.Sistema|Error fatal: El sistema se detuvo inesperadamente.

NLog is quite flexible and allows you to customize the date and time format. However, NLog doesn't directly use the date and time format 2025-05-04-11:47:05.906, as the default separators are spaces and colons (:), rather than hyphens (-).

2025-05-04-13:04:45.417|Error|Let´s log some values: 10 - or some symbols: _myInt [INT] : 10
2025-05-04-13:04:45.417|Debug|This is a debug message.
2025-05-04-13:04:45.417|Error|This is an error message.

runtimevic avatar May 04 '25 10:05 runtimevic

Hi, did you try configuring the timestamp format as described here?

bengeisler avatar May 04 '25 12:05 bengeisler

Hi, did you try configuring the timestamp format as described here?

_coreLogger
  .WriteToAds()
  .WriteToFile('c:\logs\', 'sensor_data.log')
  .MinimumLevel(TcLogLib.LogLevels.Debug)
  .TimestampFormat('YYMMDD hh:mm:ss:iii')
  .ValidTimestampsOnly()  
  .RunLogger();

If I add it method .TimestampFormat('YYMMDD hh:mm:ss:iii'), the file *.log is not created !!!

runtimevic avatar May 04 '25 19:05 runtimevic

Hello, If I add it method .TimestampFormat('YYMMDD hh:mm:ss:iii'), the file *.log is not created !!!

runtimevic avatar May 19 '25 21:05 runtimevic

Hi, I'm a bit short on time so it's unlikely that I'll look into this soon but I'd be happy to review any PR for this.

bengeisler avatar May 20 '25 05:05 bengeisler

Hi, Have you had time to look at it?

runtimevic avatar Jun 17 '25 13:06 runtimevic

Hi, no I did not work on this. I am open to reviewing PRs, though 🙂

bengeisler avatar Jun 17 '25 13:06 bengeisler

Hi, no I did not work on this. I am open to reviewing PRs, though 🙂

//AsLocalSystemTimeString := Tc2_Utilities.SYSTEMTIME_TO_STRING(THIS^.AsLocalSystemTime);


dateTimeString:=  Tc2_Utilities.SYSTEMTIME_TO_STRING(THIS^.AsLocalSystemTime);
// IF _localSystemTime.systemTime.wMonth <= 9 THEN
// 	 wMonth := CONCAT('0', TO_STRING(_localSystemTime.systemTime.wMonth));
// ELSE
// 	 wMonth := TO_STRING(_localSystemTime.systemTime.wMonth);
// 		 	 
// END_IF

// Find the position of the 3rd dash
// FOR i := 1 TO LEN(dateTimeString) DO
//     IF dateTimeString[i] = '-' THEN
//         dashCount := dashCount + 1;
//         IF dashCount = 3 THEN
//             dashPosition := i;
//             EXIT;
//         END_IF
//     END_IF
// END_FOR

// Replace the 3rd dash with a space
//IF dashPosition > 0 THEN
//    dateTimeString := REPLACE(dateTimeString, ' ', 1, dashPosition);
	dateTimeString := REPLACE(dateTimeString, ' ', 1, 11);
//END_IF

// AsLocalSystemTimeString := localSystemTimeStringBuilder.Reset()
// 							.Append(TO_STRING(_localSystemTime.systemTime.wYear))
// 							.Append('-')
// 							.Append(wMonth)
// 							.Append('-')
// 							.Append(TO_STRING(_localSystemTime.systemTime.wDay))
// 							.Append(' ')
// 							.Append(TO_STRING(_localSystemTime.systemTime.wHour))
// 							.Append(':')
// 							.Append(TO_STRING(_localSystemTime.systemTime.wMinute))
// 							.Append(':')
// 							.Append(TO_STRING(_localSystemTime.systemTime.wSecond))
// 							.Append('.')
// 							.Append(TO_STRING(_localSystemTime.systemTime.wMilliseconds))
// 							.ToString();

AsLocalSystemTimeString := dateTimeString;

To make a PR, I don't see enough changes unless it were made configurable, like the date and time within the log... This would be a PR, but you'd have to tell me how you want to be able to parameterize the date and time within the log...

runtimevic avatar Jun 18 '25 13:06 runtimevic

Hello @bengeisler, I've been reviewing the code... and I think the most reasonable approach would be to create two TimestampFormat() methods, for example: TimestampFormatFile() and TimestampFormatLog() What do you think of this idea? Is it a good solution? If you want to create the format for example for the log 'YYYY-MM-DD hh:mm:ss.iii' and instead for the file name 'YYYY-MM-DD hh_mm_ss.iii' since the ':' are not valid for creating the file name... there cannot be a single TimestampFormat() for both... If you think the possible solution is correct, then I can carry out a PR...

runtimevic avatar Jun 19 '25 06:06 runtimevic