Skip to content

Commit

Permalink
refactor file targets to core chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Oren Novotny committed Sep 22, 2012
1 parent 9fa0997 commit 755de58
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 175 deletions.
2 changes: 1 addition & 1 deletion MetroLog.NetCore.SQLite/SQLiteTarget.cs
Expand Up @@ -59,7 +59,7 @@ static SQLiteTarget()
Headers = new Dictionary<Guid, SessionHeaderItem>();
}

protected override async Task<LogWriteOperation> WriteAsync(LogWriteContext context, LogEventInfo entry)
protected override async Task<LogWriteOperation> WriteAsyncCore(LogWriteContext context, LogEventInfo entry)
{
await EnsureInitialize();

Expand Down
23 changes: 4 additions & 19 deletions MetroLog.NetCore/FileSnapshotTarget.cs
Expand Up @@ -10,7 +10,7 @@

namespace MetroLog.Targets
{
public class FileSnapshotTarget : FileTargetBase
public class FileSnapshotTarget : WinRTFileTarget
{
public FileSnapshotTarget()
: this(new FileSnapshotLayout())
Expand All @@ -25,27 +25,12 @@ public FileSnapshotTarget(Layout layout)
this.FileNamingParameters.IncludeSession = false;
this.FileNamingParameters.IncludeSequence = true;
this.FileNamingParameters.IncludeTimestamp = FileTimestampMode.DateTime;
FileNamingParameters.CreationMode = FileCreationMode.ReplaceIfExisting;
}

protected override async Task<LogWriteOperation> WriteAsync(LogWriteContext context, LogEventInfo entry)
protected override Task WriteTextToFileCore(IStorageFile file, string contents)
{
var folder = await EnsureInitializedAsync();
if (folder == null)
return new LogWriteOperation(this, entry, false);

// cleanup...
await this.CheckCleanupAsync(folder);

// create the file...
var filename = this.FileNamingParameters.GetFilename(context, entry);
var file = await folder.CreateFileAsync(filename).AsTask();

// write...
string buf = this.Layout.GetFormattedString(context, entry);
await FileIO.WriteTextAsync(file, buf);

// return...
return new LogWriteOperation(this, entry, true);
return FileIO.WriteTextAsync(file, contents).AsTask();
}
}
}
22 changes: 4 additions & 18 deletions MetroLog.NetCore/FileStreamingTarget.cs
Expand Up @@ -12,7 +12,7 @@ namespace MetroLog.Targets
/// <summary>
/// Defines a target that will append messages to a single file.
/// </summary>
public class FileStreamingTarget : FileTargetBase
public class FileStreamingTarget : WinRTFileTarget
{
public FileStreamingTarget()
: this(new SingleLineLayout())
Expand All @@ -27,26 +27,12 @@ public FileStreamingTarget(Layout layout)
this.FileNamingParameters.IncludeSequence = false;
this.FileNamingParameters.IncludeSession = false;
this.FileNamingParameters.IncludeTimestamp = FileTimestampMode.Date;
FileNamingParameters.CreationMode = FileCreationMode.AppendIfExisting;
}

protected override async Task<LogWriteOperation> WriteAsync(LogWriteContext context, LogEventInfo entry)
protected override Task WriteTextToFileCore(IStorageFile file, string contents)
{
var folder = await FileSnapshotTarget.EnsureInitializedAsync();

// cleanup...
await this.CheckCleanupAsync(folder);

// write...
var filename = this.FileNamingParameters.GetFilename(context, entry);
var file = await folder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);

// need to append a session header...

// append...
await FileIO.AppendTextAsync(file, this.Layout.GetFormattedString(context, entry) + "\r\n");

// return...
return new LogWriteOperation(this, entry, true);
return FileIO.AppendTextAsync(file, contents + Environment.NewLine).AsTask();
}
}
}
132 changes: 0 additions & 132 deletions MetroLog.NetCore/FileTargetBase.cs

This file was deleted.

2 changes: 1 addition & 1 deletion MetroLog.NetCore/MetroLog.NetCore.csproj
Expand Up @@ -114,13 +114,13 @@
<Compile Include="FileSnapshotTarget.cs" />
<Compile Include="FileNamingMode.cs" />
<Compile Include="FileStreamingTarget.cs" />
<Compile Include="FileTargetBase.cs" />
<Compile Include="LoggingEnvironment.cs" />
<Compile Include="LogConfigurator.cs" />
<Compile Include="GlobalCrashHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="LazyFlushManager.cs" />
<Compile Include="StorageFilePackager.cs" />
<Compile Include="WinRTFileTarget.cs" />
<Compile Include="XamlExtensionMethods.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
107 changes: 107 additions & 0 deletions MetroLog.NetCore/WinRTFileTarget.cs
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using MetroLog.Layouts;
using MetroLog.Targets;
using Windows.Storage;

namespace MetroLog
{
public abstract class WinRTFileTarget : FileTargetBase
{
private static StorageFolder _logFolder = null;


protected WinRTFileTarget(Layout layout) : base(layout)
{
}

public static async Task<StorageFolder> EnsureInitializedAsync()
{
var folder = _logFolder;
if (folder == null)
{
StorageFolder logFolder = null;
var root = ApplicationData.Current.LocalFolder;
try
{
logFolder = await root.GetFolderAsync(LogFolderName);
}
catch (FileNotFoundException)
{
}

// if...
if (logFolder == null)
{
try
{
logFolder = await root.CreateFolderAsync(LogFolderName, CreationCollisionOption.OpenIfExists);
}
catch (Exception)
{
}

// if we get into trouble here, try and load it again... (something else should have created it)...
if (logFolder == null)
logFolder = await root.GetFolderAsync(LogFolderName);
}

// store it - but only if we have one...
if (logFolder != null)
Interlocked.CompareExchange<StorageFolder>(ref _logFolder, logFolder, null);
}
return _logFolder;
}


protected override Task EnsureInitialized()
{
return EnsureInitializedAsync();
}

protected override async Task DoCleanup(Regex pattern, DateTime threshold)
{

var toDelete = new List<StorageFile>();
foreach (var file in await _logFolder.GetFilesAsync())
{
if (pattern.Match(file.Name).Success && file.DateCreated <= threshold)
toDelete.Add(file);
}

// walk...
foreach (var file in toDelete)
{
try
{
await file.DeleteAsync();
}
catch (Exception ex)
{
InternalLogger.Current.Warn(string.Format("Failed to delete '{0}'.", file.Path), ex);
}
}
}

protected sealed override async Task<LogWriteOperation> DoWriteAsync(string fileName, string contents, LogEventInfo entry)
{
// write...

var file = await _logFolder.CreateFileAsync(fileName, FileNamingParameters.CreationMode == FileCreationMode.AppendIfExisting ? CreationCollisionOption.OpenIfExists : CreationCollisionOption.ReplaceExisting);

// Write contents
await WriteTextToFileCore(file, contents);

// return...
return new LogWriteOperation(this, entry, true);
}

protected abstract Task WriteTextToFileCore(IStorageFile file, string contents);
}
}
2 changes: 2 additions & 0 deletions MetroLog/MetroLog.csproj
Expand Up @@ -146,7 +146,9 @@
<Compile Include="Targets\BufferedTarget.cs" />
<Compile Include="Targets\DebugTarget.cs" />
<Compile Include="Targets\EtwTarget.cs" />
<Compile Include="Targets\FileCreationMode.cs" />
<Compile Include="Targets\FileNamingParameters.cs" />
<Compile Include="Targets\FileTargetBase.cs" />
<Compile Include="Targets\FileTimestampMoe.cs" />
<Compile Include="Targets\HttpClientEventHandler.cs" />
<Compile Include="Targets\IReadableTarget.cs" />
Expand Down
2 changes: 2 additions & 0 deletions MetroLog/Targets/AsyncTarget.cs
Expand Up @@ -13,5 +13,7 @@ protected AsyncTarget(Layout layout)
: base(layout)
{
}


}
}
4 changes: 2 additions & 2 deletions MetroLog/Targets/BufferedTarget.cs
Expand Up @@ -8,7 +8,7 @@

namespace MetroLog.Targets
{
public abstract class BufferedTarget : Target, ILazyFlushable
public abstract class BufferedTarget : AsyncTarget, ILazyFlushable
{
private List<LogEventInfo> Buffer { get; set; }
private object _lock = new object();
Expand All @@ -24,7 +24,7 @@ public BufferedTarget(Layout layout, int threshold)
this.Buffer = new List<LogEventInfo>();
}

protected internal override sealed Task<LogWriteOperation> WriteAsync(LogWriteContext context, LogEventInfo entry)
protected override sealed Task<LogWriteOperation> WriteAsyncCore(LogWriteContext context, LogEventInfo entry)
{
try
{
Expand Down
14 changes: 14 additions & 0 deletions MetroLog/Targets/FileCreationMode.cs
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MetroLog.Targets
{
public enum FileCreationMode
{
ReplaceIfExisting,
AppendIfExisting
}
}

0 comments on commit 755de58

Please sign in to comment.