Skip to content

Commit

Permalink
Updated logger to not throw exceptions if it can't write to the log f…
Browse files Browse the repository at this point in the history
…ile, updated readme, and bumped version.
  • Loading branch information
garrettpauls committed Aug 5, 2016
1 parent 6d96a74 commit 7644939
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
7 changes: 7 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ errors occur, they will be logged to a file in the log subdirectory.
The changelog of all versions.
Issue details can be found by number in the [issue tracker](https://github.com/garrettpauls/VSGallery.AtomGenerator/issues).

## v1.0.2

* Pulled in ChrisMaddock's changes:
* Fixed URI format exceptions when targeting network paths.
* Added return code 0 for success, 1 for error.
* Logger no longer throws an exception if it can't write to the file.

## v1.0.1

* Issue #1 - zip entry names for images are normalized to support both / and \ in the manifest file.
Expand Down
50 changes: 39 additions & 11 deletions VSGallery.AtomGenerator/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,32 @@ public sealed class Logger
{
private readonly string mFile;

private bool mHasLoggedFileAccessError;

private Logger(string file)
{
mFile = file;
}

public static Logger Create(string file)
private void _WriteLine(bool isError, string value)
{
var logDir = Directory.GetParent(file);
if (!logDir.Exists)
_WriteToConsole(isError, value);

try
{
logDir.Create();
var header = isError ? "ERROR" : "INFO";
File.AppendAllText(mFile, $"{header}: {value}\r\n");
}
catch(Exception ex)
{
if(!mHasLoggedFileAccessError)
{
mHasLoggedFileAccessError = true;

return new Logger(file);
_WriteToConsole(true, $"Could not write log to file (future write failures are not logged): {mFile}");
_WriteToConsole(true, ex.ToString());
}
}
}

public void Error(string message)
Expand All @@ -30,7 +42,7 @@ public void Error(string message)

public void Error(string message, Exception ex)
{
if (ex != null)
if(ex != null)
{
message += $@"
--------------------------------------------------
Expand All @@ -51,18 +63,34 @@ private static string _ToString(Exception exception)
return exception.ToString();
}

private void _WriteLine(bool isError, string value)
private static void _WriteToConsole(bool isError, string value)
{
var color = Console.ForegroundColor;
if (isError)
if(isError)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
}
Console.WriteLine(value);
Console.ForegroundColor = color;
}

public static Logger Create(string file)
{
var logDir = Directory.GetParent(file);
if(!logDir.Exists)
{
try
{
logDir.Create();
}
catch(Exception ex)
{
_WriteToConsole(true, $"Could not create directory for logs: {logDir.FullName}");
_WriteToConsole(true, ex.Message);
}
}

var header = isError ? "ERROR" : "INFO";
File.AppendAllText(mFile, $"{header}: {value}\r\n");
return new Logger(file);
}
}
}
}
6 changes: 3 additions & 3 deletions VSGallery.AtomGenerator/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyTitle("VSGallery.AtomGenerator")]
[assembly: AssemblyDescription("Generates an atom feed for custom visual studio galleries.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VSGallery.AtomGenerator")]
[assembly: AssemblyCopyright("Copyright © Garrett Pauls 2015")]
[assembly: AssemblyCopyright("Copyright © Garrett Pauls 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
Expand Down

0 comments on commit 7644939

Please sign in to comment.