Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor file targets to core chunks
- Loading branch information
Oren Novotny
committed
Sep 22, 2012
1 parent
9fa0997
commit 755de58
Showing
14 changed files
with
228 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,7 @@ protected AsyncTarget(Layout layout) | |
: base(layout) | ||
{ | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
Oops, something went wrong.