Skip to content

Commit 2c1150a

Browse files
authored
Performance improvements for file logging (#9655)
1 parent c4b1c9c commit 2c1150a

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/WebJobs.Script/Diagnostics/FileLogger.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,8 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
9292
try
9393
{
9494
_fileWriter.AppendLine(formattedMessage);
95-
96-
// flush errors immediately
97-
if (logLevel == LogLevel.Error || exception != null)
98-
{
99-
_fileWriter.Flush();
100-
}
10195
}
102-
catch (Exception)
96+
catch (Exception ex) when (!ex.IsFatal())
10397
{
10498
// Make sure the Logger doesn't throw if there are Exceptions (disk full, etc).
10599
}

src/WebJobs.Script/Diagnostics/FileWriter.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal class FileWriter : IFileWriter, IDisposable
3030
private Timer _flushTimer;
3131
private ConcurrentQueue<string> _logBuffer = new ConcurrentQueue<string>();
3232

33-
public FileWriter(string logFilePath)
33+
public FileWriter(string logFilePath, int flushIntervalMs = LogFlushIntervalMs)
3434
{
3535
_logFilePath = logFilePath;
3636
_instanceId = GetInstanceId();
@@ -39,8 +39,8 @@ public FileWriter(string logFilePath)
3939
// start a timer to flush accumulated logs in batches
4040
_flushTimer = new Timer
4141
{
42-
AutoReset = true,
43-
Interval = LogFlushIntervalMs
42+
AutoReset = false,
43+
Interval = flushIntervalMs
4444
};
4545
_flushTimer.Elapsed += OnFlushLogs;
4646
_flushTimer.Start();
@@ -171,7 +171,14 @@ public void AppendLine(string line)
171171

172172
private void OnFlushLogs(object sender, ElapsedEventArgs e)
173173
{
174-
Flush();
174+
try
175+
{
176+
Flush();
177+
}
178+
finally
179+
{
180+
_flushTimer.Start();
181+
}
175182
}
176183

177184
/// <summary>

test/WebJobs.Script.Tests/Diagnostics/FileWriterTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,28 @@ public void Flush_ReusesLastFile()
182182
Assert.Equal(3, fileLines.Length);
183183
}
184184

185+
[Fact]
186+
public async Task LogsAreFlushedOnTimer()
187+
{
188+
FileWriter fileWriter = new FileWriter(_logFilePath, flushIntervalMs: 10);
189+
190+
// timer should elapse 50 times during this test
191+
int numLogs = 100;
192+
for (int i = 0; i < numLogs; i++)
193+
{
194+
fileWriter.AppendLine($"Test message {Thread.CurrentThread.ManagedThreadId} {i}");
195+
await Task.Delay(5);
196+
}
197+
198+
var directory = new DirectoryInfo(_logFilePath);
199+
int count = directory.EnumerateFiles().Count();
200+
Assert.Equal(1, count);
201+
202+
string logFile = directory.EnumerateFiles().First().FullName;
203+
string[] fileLines = File.ReadAllLines(logFile);
204+
Assert.Equal(numLogs, fileLines.Length);
205+
}
206+
185207
[Fact]
186208
public async Task Flush_ThrottlesLogs()
187209
{

0 commit comments

Comments
 (0)