Permalink
Browse files

Add support for getting a zip file IStorageFile directly and a cleanu…

…p mechanism.
  • Loading branch information...
onovotny committed Nov 1, 2012
1 parent f5f1df0 commit 8e9a1e7f7967604b0a6c3be55b1424a9cd73ab11
@@ -1,10 +1,13 @@
-using System;
+extern alias pcl;
+
+using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using MetroLog.Internal;
using MetroLog.Targets;
using Windows.Storage;
using Xunit;
@@ -39,21 +42,32 @@ public class FileSnapshotTargetTests
[Fact]
public async Task TestGetZipFile()
{
- var target = new FileSnapshotTarget();
+ var manager = pcl::MetroLog.LogManagerFactory.CreateLogManager() as IWinRTLogManager;
+
+ var logger = (ILoggerAsync)manager.GetLogger("test");
+
// send through a log entry...
- var op = await target.WriteAsync(new LogWriteContext(),
- new LogEventInfo(LogLevel.Fatal, "TestLogger", "Testing file write...", new InvalidOperationException("An exception message...")));
+ var op = await logger.FatalAsync("Testing file write...", new InvalidOperationException("An exception message..."));
- var folder = await FileSnapshotTarget.EnsureInitializedAsync();
+ var file = await manager.GetCompressedLogFile();
+
+ Assert.True(file.Name.EndsWith(".zip"));
- var str = await target.GetCompressedLogs();
+ var target = manager.DefaultConfiguration.GetTargets().OfType<FileTargetBase>().First();
- var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("logs.zip", CreationCollisionOption.ReplaceExisting);
+ await target.ForceCleanupAsync();
- using (var stream = (await file.OpenAsync(FileAccessMode.ReadWrite)).AsStream())
+ var exceptionThrow = false;
+ try
{
- await str.CopyToAsync(stream);
+ var str = await file.OpenReadAsync();
}
+ catch (FileNotFoundException)
+ {
+ exceptionThrow = true;
+ }
+
+ Assert.True(exceptionThrow);
}
}
}
@@ -1,4 +1,6 @@
-using System;
+extern alias pcl;
+
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -23,7 +25,7 @@ public static void Configure()
e.Handled = true;
// go...
- var log = (ILoggerAsync)LogManagerFactory.DefaultLogManager.GetLogger<Application>();
+ var log = (ILoggerAsync)pcl::MetroLog.LogManagerFactory.DefaultLogManager.GetLogger<Application>();
await log.FatalAsync("The application crashed: " + e.Message, e.Exception);
// if we're aborting, fake a suspend to flush the targets...
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Windows.Storage;
+
+namespace MetroLog
+{
+ public interface IWinRTLogManager : ILogManager
+ {
+ Task<IStorageFile> GetCompressedLogFile();
+ }
+}
@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using MetroLog.Internal;
+using Windows.Storage;
+
+namespace MetroLog
+{
+ class LogManager : LogManagerBase, IWinRTLogManager
+ {
+ public LogManager(LoggingConfiguration configuration) : base(configuration)
+ {
+ }
+
+ public async Task<IStorageFile> GetCompressedLogFile()
+ {
+ var stream = await GetCompressedLogs();
+
+ if (stream != null)
+ {
+ // create a temp file
+ var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(
+ string.Format("Log - {0}.zip", DateTime.UtcNow.ToString("yyyy-MM-dd HHmmss", CultureInfo.InvariantCulture)), CreationCollisionOption.ReplaceExisting);
+
+ using (var ras = (await file.OpenAsync(FileAccessMode.ReadWrite)).AsStreamForWrite())
+ {
+ await stream.CopyToAsync(ras);
+ }
+
+ stream.Dispose();
+
+ return file;
+ }
+
+ return null;
+ }
+ }
+}
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MetroLog
+{
+ class LogManagerFactory : ILogManagerFactory
+ {
+ public ILogManager Create(LoggingConfiguration configuration)
+ {
+ return new LogManager(configuration);
+ }
+ }
+}
@@ -105,6 +105,7 @@
<ProjectReference Include="..\MetroLog\MetroLog.csproj">
<Project>{65e2062e-aac2-4054-aae6-d6764040c769}</Project>
<Name>MetroLog</Name>
+ <Aliases>global, pcl</Aliases>
</ProjectReference>
</ItemGroup>
<ItemGroup>
@@ -114,12 +115,14 @@
<Compile Include="FileSnapshotTarget.cs" />
<Compile Include="FileNamingMode.cs" />
<Compile Include="FileStreamingTarget.cs" />
+ <Compile Include="IWinRTLogManager.cs" />
<Compile Include="LoggingEnvironment.cs" />
<Compile Include="LogConfigurator.cs" />
<Compile Include="GlobalCrashHandler.cs" />
+ <Compile Include="LogManager.cs" />
+ <Compile Include="LogManagerFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="LazyFlushManager.cs" />
- <Compile Include="StorageFilePackager.cs" />
<Compile Include="WinRTFileTarget.cs" />
<Compile Include="XamlApplicationState.cs" />
<Compile Include="XamlExtensionMethods.cs" />
@@ -1,21 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using Windows.Storage;
-
-namespace MetroLog.Packaging
-{
- /// <summary>
- /// Describes a class that can package up events for transmission.
- /// </summary>
- public class StorageFilePackager
- {
- public StorageFilePackager()
- {
- }
-
- }
-}
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
@@ -10,6 +11,7 @@
using MetroLog.Layouts;
using MetroLog.Targets;
using Windows.Storage;
+using Windows.Storage.Search;
namespace MetroLog
{
@@ -43,8 +45,7 @@ protected WinRTFileTarget(Layout layout) : base(layout)
return ms;
}
-
-
+
protected override Task EnsureInitialized()
{
return EnsureInitializedAsync();
@@ -60,6 +61,18 @@ protected override Task EnsureInitialized()
toDelete.Add(file);
}
+
+ var qo = new QueryOptions(CommonFileQuery.DefaultQuery, new [] {".zip"})
+ {
+ FolderDepth = FolderDepth.Shallow,
+ UserSearchFilter = "System.FileName:~<\"Log -\""
+ };
+
+ var query = ApplicationData.Current.TemporaryFolder.CreateFileQueryWithOptions(qo);
+
+ var oldLogs = await query.GetFilesAsync();
+ toDelete.AddRange(oldLogs);
+
// walk...
foreach (var file in toDelete)
{
@@ -1,4 +1,6 @@
-using System;
+extern alias pcl;
+
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -20,7 +22,7 @@ public static class XamlExtensionMethods
/// <returns></returns>
public static ILogger GetLogger(this UserControl control, LoggingConfiguration config = null)
{
- return LogManagerFactory.DefaultLogManager.GetLogger(control.GetType(), config);
+ return pcl::MetroLog.LogManagerFactory.DefaultLogManager.GetLogger(control.GetType(), config);
}
}
}
@@ -99,7 +99,10 @@ public static Task<ZipArchive> OpenRead(Stream archive)
using (Stream stream = (await sourceFile.OpenReadAsync()).AsStream())
{
ZipArchiveEntry zipArchiveEntry = compressionLevel.HasValue ? destination.CreateEntry(entryName, compressionLevel.Value) : destination.CreateEntry(entryName);
- DateTime dateTime = sourceFile.DateCreated.UtcDateTime; //File.GetLastWriteTime(sourceFileName);
+
+ var props = await sourceFile.GetBasicPropertiesAsync();
+
+ DateTime dateTime = props.DateModified.UtcDateTime;
if (dateTime.Year < 1980 || dateTime.Year > 2107)
dateTime = new DateTime(1980, 1, 1, 0, 0, 0);
zipArchiveEntry.LastWriteTime = (DateTimeOffset)dateTime;
@@ -20,7 +20,7 @@ public class BrokenTargetTests
config.AddTarget(LogLevel.Trace, LogLevel.Fatal, new BrokenTarget());
config.AddTarget(LogLevel.Trace, LogLevel.Fatal, testTarget);
- var target = new LogManager(config);
+ var target = new LogManagerBase(config);
// this should ignore errors in the broken target and flip down to the working target...
var logger = (Logger)target.GetLogger("Foobar");
@@ -113,7 +113,7 @@ public void TestTimestampDate()
// check...
var filename = naming.GetFilename(new LogWriteContext(), info);
- Assert.Equal(string.Format("Log - {0}.log", LogManager.GetDateTime().ToString("yyyyMMdd")), filename);
+ Assert.Equal(string.Format("Log - {0}.log", LogManagerBase.GetDateTime().ToString("yyyyMMdd")), filename);
}
[Fact]
@@ -133,7 +133,7 @@ public void TestTimestampTime()
// check...
var filename = naming.GetFilename(new LogWriteContext(), info);
- Assert.Equal(string.Format("Log - {0}.log", LogManager.GetDateTime().ToString("HHmmss")), filename);
+ Assert.Equal(string.Format("Log - {0}.log", LogManagerBase.GetDateTime().ToString("HHmmss")), filename);
}
[Fact]
@@ -153,7 +153,7 @@ public void TestTimestampBoth()
// check...
var filename = naming.GetFilename(new LogWriteContext(), info);
- Assert.Equal(string.Format("Log - {0}.log", LogManager.GetDateTime().ToString("yyyyMMdd HHmmss")), filename);
+ Assert.Equal(string.Format("Log - {0}.log", LogManagerBase.GetDateTime().ToString("yyyyMMdd HHmmss")), filename);
}
[Fact]
@@ -174,7 +174,7 @@ public void TestEverythingOn()
// check...
var context = new LogWriteContext();
var filename = naming.GetFilename(context, info);
- Assert.Equal(string.Format("Log - INFO - foobar - {0} - {1} - {2}.log", LogManager.GetDateTime().ToString("yyyyMMdd HHmmss"),
+ Assert.Equal(string.Format("Log - INFO - foobar - {0} - {1} - {2}.log", LogManagerBase.GetDateTime().ToString("yyyyMMdd HHmmss"),
context.Environment.SessionId, info.SequenceID), filename);
}
Oops, something went wrong.

0 comments on commit 8e9a1e7

Please sign in to comment.