Skip to content

Commit b956ede

Browse files
Cache DateTimeFormatInfo to reduce allocations in every Logger.Log call
1 parent 4371ae1 commit b956ede

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

fCraft/System/Logger.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Diagnostics;
5+
using System.Globalization;
56
using System.IO;
67
using System.Linq;
78
using System.Net;
@@ -32,6 +33,7 @@ public static class Logger {
3233

3334
static readonly string SessionStart = DateTime.Now.ToString( LongDateFormat ); // localized
3435
static readonly Queue<string> RecentMessages = new Queue<string>();
36+
static DateTimeFormatInfo formatter = CultureInfo.InvariantCulture.DateTimeFormat;
3537
const int MaxRecentMessages = 25;
3638

3739
/// <summary> Name of the file that log messages are currently being written to.
@@ -42,7 +44,7 @@ public static string CurrentLogFileName {
4244
case LogSplittingType.SplitBySession:
4345
return SessionStart + ".log";
4446
case LogSplittingType.SplitByDay:
45-
return DateTime.Now.ToString( ShortDateFormat ) + ".log"; // localized
47+
return DateTime.Now.ToString( ShortDateFormat, formatter ) + ".log"; // localized
4648
default:
4749
return DefaultLogFileName;
4850
}
@@ -116,7 +118,7 @@ public static void Log( LogType type, [NotNull] string message, [NotNull] params
116118
if( !Enabled ) return;
117119

118120
message = Color.StripColors( message, true );
119-
string line = DateTime.Now.ToString( TimeFormat ) + " > " + GetPrefix( type ) + message; // localized
121+
string line = DateTime.Now.ToString( TimeFormat, formatter ) + " > " + GetPrefix( type ) + message; // localized
120122

121123
lock( LogLock ) {
122124
RaiseLoggedEvent( message, line, type );
@@ -133,7 +135,7 @@ public static void Log( LogType type, [NotNull] string message, [NotNull] params
133135
} catch( Exception ex ) {
134136
string errorMessage = "Logger.Log: " + ex;
135137
line = String.Format( "{0} > {1}{2}",
136-
DateTime.Now.ToString( TimeFormat ),// localized
138+
DateTime.Now.ToString( TimeFormat, formatter ),// localized
137139
GetPrefix( LogType.Error ),
138140
errorMessage );
139141
RaiseLoggedEvent( errorMessage,

0 commit comments

Comments
 (0)