@@ -14,12 +14,14 @@ public class FileTraceWriter : TraceWriter
14
14
{
15
15
private object _syncLock = new object ( ) ;
16
16
private readonly string _logFilePath ;
17
+ private readonly string _instanceId ;
17
18
private const long _maxLogFileSizeBytes = 5 * 1024 * 1024 ;
18
19
private FileInfo _currentLogFileInfo ;
19
20
20
21
public FileTraceWriter ( string logFilePath , TraceLevel level ) : base ( level )
21
22
{
22
23
_logFilePath = logFilePath ;
24
+ _instanceId = GetInstanceId ( ) ;
23
25
24
26
DirectoryInfo directory = new DirectoryInfo ( logFilePath ) ;
25
27
if ( ! directory . Exists )
@@ -29,7 +31,8 @@ public FileTraceWriter(string logFilePath, TraceLevel level): base (level)
29
31
else
30
32
{
31
33
// 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 ( ) ;
33
36
}
34
37
35
38
if ( _currentLogFileInfo == null )
@@ -45,7 +48,6 @@ public override void Trace(TraceEvent traceEvent)
45
48
throw new ArgumentNullException ( "traceEvent" ) ;
46
49
}
47
50
48
- // TODO: figure out the right log file format
49
51
// TODO: buffer logs and write only periodically
50
52
AppendLine ( traceEvent . Message ) ;
51
53
if ( traceEvent . Exception != null )
@@ -77,9 +79,10 @@ protected virtual void AppendLine(string line)
77
79
return ;
78
80
}
79
81
82
+ // TODO: figure out the right log file format
80
83
line = string . Format ( CultureInfo . InvariantCulture , "{0} {1}\r \n " , DateTime . Now . ToString ( "s" , CultureInfo . InvariantCulture ) , line . Trim ( ) ) ;
81
84
82
- // TODO: fix this locking issue
85
+ // TODO: optimize this locking
83
86
try
84
87
{
85
88
lock ( _syncLock )
@@ -109,8 +112,23 @@ protected virtual void AppendLine(string line)
109
112
110
113
private void SetNewLogFile ( )
111
114
{
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 ) ) ;
113
118
_currentLogFileInfo = new FileInfo ( filePath ) ;
114
119
}
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
+ }
115
133
}
116
134
}
0 commit comments