@@ -14,12 +14,14 @@ public class FileTraceWriter : TraceWriter
1414 {
1515 private object _syncLock = new object ( ) ;
1616 private readonly string _logFilePath ;
17+ private readonly string _instanceId ;
1718 private const long _maxLogFileSizeBytes = 5 * 1024 * 1024 ;
1819 private FileInfo _currentLogFileInfo ;
1920
2021 public FileTraceWriter ( string logFilePath , TraceLevel level ) : base ( level )
2122 {
2223 _logFilePath = logFilePath ;
24+ _instanceId = GetInstanceId ( ) ;
2325
2426 DirectoryInfo directory = new DirectoryInfo ( logFilePath ) ;
2527 if ( ! directory . Exists )
@@ -29,7 +31,8 @@ public FileTraceWriter(string logFilePath, TraceLevel level): base (level)
2931 else
3032 {
3133 // get the last log file written to (or null)
32- _currentLogFileInfo = directory . GetFiles ( ) . OrderByDescending ( p => p . LastWriteTime ) . FirstOrDefault ( ) ;
34+ string pattern = string . Format ( CultureInfo . InvariantCulture , "*-{0}.log" , _instanceId ) ;
35+ _currentLogFileInfo = directory . GetFiles ( pattern ) . OrderByDescending ( p => p . LastWriteTime ) . FirstOrDefault ( ) ;
3336 }
3437
3538 if ( _currentLogFileInfo == null )
@@ -45,7 +48,6 @@ public override void Trace(TraceEvent traceEvent)
4548 throw new ArgumentNullException ( "traceEvent" ) ;
4649 }
4750
48- // TODO: figure out the right log file format
4951 // TODO: buffer logs and write only periodically
5052 AppendLine ( traceEvent . Message ) ;
5153 if ( traceEvent . Exception != null )
@@ -77,9 +79,10 @@ protected virtual void AppendLine(string line)
7779 return ;
7880 }
7981
82+ // TODO: figure out the right log file format
8083 line = string . Format ( CultureInfo . InvariantCulture , "{0} {1}\r \n " , DateTime . Now . ToString ( "s" , CultureInfo . InvariantCulture ) , line . Trim ( ) ) ;
8184
82- // TODO: fix this locking issue
85+ // TODO: optimize this locking
8386 try
8487 {
8588 lock ( _syncLock )
@@ -109,8 +112,23 @@ protected virtual void AppendLine(string line)
109112
110113 private void SetNewLogFile ( )
111114 {
112- string filePath = Path . Combine ( _logFilePath , string . Format ( CultureInfo . InvariantCulture , "{0}.log" , Guid . NewGuid ( ) ) ) ;
115+ // we include a machine identifier in the log file name to ensure we don't have any
116+ // log file contention between scaled out instances
117+ string filePath = Path . Combine ( _logFilePath , string . Format ( CultureInfo . InvariantCulture , "{0}-{1}.log" , Guid . NewGuid ( ) , _instanceId ) ) ;
113118 _currentLogFileInfo = new FileInfo ( filePath ) ;
114119 }
120+
121+ private static string GetInstanceId ( )
122+ {
123+ string instanceId = System . Environment . GetEnvironmentVariable ( "WEBSITE_INSTANCE_ID" ) ;
124+ if ( string . IsNullOrEmpty ( instanceId ) )
125+ {
126+ instanceId = Environment . MachineName ;
127+ }
128+
129+ instanceId = instanceId . Length > 10 ? instanceId . Substring ( 0 , 10 ) : instanceId ;
130+
131+ return instanceId . ToLowerInvariant ( ) ;
132+ }
115133 }
116134}
0 commit comments