Skip to content
Permalink
Browse files

Cache DateTimeFormatInfo to reduce allocations in every Logger.Log call

  • Loading branch information...
UnknownShadow200 committed Oct 22, 2017
1 parent 4371ae1 commit b956ede1c90de8a3b14c006929c330d5085bb912
Showing with 5 additions and 3 deletions.
  1. +5 −3 fCraft/System/Logger.cs
@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
@@ -32,6 +33,7 @@ public static class Logger {

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

/// <summary> Name of the file that log messages are currently being written to.
@@ -42,7 +44,7 @@ public static class Logger {
case LogSplittingType.SplitBySession:
return SessionStart + ".log";
case LogSplittingType.SplitByDay:
return DateTime.Now.ToString( ShortDateFormat ) + ".log"; // localized
return DateTime.Now.ToString( ShortDateFormat, formatter ) + ".log"; // localized
default:
return DefaultLogFileName;
}
@@ -116,7 +118,7 @@ public static class Logger {
if( !Enabled ) return;

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

lock( LogLock ) {
RaiseLoggedEvent( message, line, type );
@@ -133,7 +135,7 @@ public static class Logger {
} catch( Exception ex ) {
string errorMessage = "Logger.Log: " + ex;
line = String.Format( "{0} > {1}{2}",
DateTime.Now.ToString( TimeFormat ),// localized
DateTime.Now.ToString( TimeFormat, formatter ),// localized
GetPrefix( LogType.Error ),
errorMessage );
RaiseLoggedEvent( errorMessage,

0 comments on commit b956ede

Please sign in to comment.
You can’t perform that action at this time.