Skip to content

Commit

Permalink
Add rudimentary sized base log file rolling
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewc committed Jan 28, 2016
1 parent eae496a commit a82e7e3
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/WebJobs.Script/FileTraceWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Azure.WebJobs.Host;

namespace Microsoft.Azure.WebJobs.Script
Expand All @@ -9,13 +10,28 @@ public class FileTraceWriter : TraceWriter
{
private object _syncLock = new object();
private readonly string _logFilePath;
private readonly string _logFileName;
private const long _maxLogFileSizeBytes = 5 * 1024 * 1024;
private FileInfo _currentLogFileInfo;

public FileTraceWriter(string logFilePath, TraceLevel level): base (level)
{
_logFilePath = logFilePath;
Directory.CreateDirectory(logFilePath);
_logFileName = Path.Combine(_logFilePath, string.Format("{0}.log", Guid.NewGuid()));

DirectoryInfo directory = new DirectoryInfo(logFilePath);
if (!directory.Exists)
{
Directory.CreateDirectory(logFilePath);
}
else
{
// get the last log file written to (or null)
_currentLogFileInfo = directory.GetFiles().OrderByDescending(p => p.LastWriteTime).FirstOrDefault();
}

if (_currentLogFileInfo == null)
{
SetNewLogFile();
}
}

public override void Trace(TraceEvent traceEvent)
Expand Down Expand Up @@ -47,15 +63,27 @@ public override void Trace(TraceEvent traceEvent)

protected virtual void AppendLine(string line)
{
// TODO: Once the log file exceeds a certain size, we want to create a new one

line = string.Format("{0} {1}\r\n", DateTime.Now.ToString("s"), line.Trim());

// TODO: fix this locking issue
lock (_syncLock)
{
File.AppendAllText(_logFileName, line);
File.AppendAllText(_currentLogFileInfo.FullName, line);
}

// TODO: Need to optimize this, so we only do the check every
// so often
_currentLogFileInfo.Refresh();
if (_currentLogFileInfo.Length > _maxLogFileSizeBytes)
{
SetNewLogFile();
}
}

private void SetNewLogFile()
{
string filePath = Path.Combine(_logFilePath, string.Format("{0}.log", Guid.NewGuid()));
_currentLogFileInfo = new FileInfo(filePath);
}
}
}

0 comments on commit a82e7e3

Please sign in to comment.