Skip to content

Commit

Permalink
ExceptionHandling and ExceptionHandling with Logging
Browse files Browse the repository at this point in the history
Signed-off-by: PulsarBlow <pulsarblow@gmail.com>
  • Loading branch information
PulsarBlow committed Mar 2, 2015
1 parent 1db6430 commit 05c06a8
Show file tree
Hide file tree
Showing 28 changed files with 2,709 additions and 21 deletions.
6 changes: 3 additions & 3 deletions GlobalAssemblyInfos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
[assembly: AssemblyConfiguration("Release")]
#endif

[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyInformationalVersion("1.3.0")]
[assembly: AssemblyVersion("1.4.2.0")]
[assembly: AssemblyFileVersion("1.4.2.0")]
[assembly: AssemblyInformationalVersion("1.4.2")]
4 changes: 4 additions & 0 deletions Nuget-Pub/pack.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ ECHO Delete Existing Packages
for /r %%f in (*.nupkg) do del %%f
ECHO Packing Projects
NuGet Pack ..\Source\SuperMassive\SuperMassive.csproj -OutputDirectory ".\SuperMassive" -Build -IncludeReferencedProjects -Prop Configuration=Release -Symbols
NuGet Pack ..\Source\SuperMassive.ExceptionHandling\SuperMassive.ExceptionHandling.csproj -OutputDirectory ".\SuperMassive.ExceptionHandling" -Build -IncludeReferencedProjects -Prop Configuration=Release -Symbols
NuGet Pack ..\Source\SuperMassive.ExceptionHandling.Logging\SuperMassive.ExceptionHandling.Logging.csproj -OutputDirectory ".\SuperMassive.ExceptionHandling.Logging" -Build -IncludeReferencedProjects -Prop Configuration=Release -Symbols
NuGet Pack ..\Source\SuperMassive.Fakers\SuperMassive.Fakers.csproj -OutputDirectory ".\SuperMassive.Fakers" -Build -IncludeReferencedProjects -Prop Configuration=Release -Symbols
NuGet Pack ..\Source\SuperMassive.Logging\SuperMassive.Logging.csproj -OutputDirectory ".\SuperMassive.Logging" -Build -IncludeReferencedProjects -Prop Configuration=Release -Symbols
NuGet Pack ..\Source\SuperMassive.Logging.AzureTable\SuperMassive.Logging.AzureTable.csproj -OutputDirectory ".\SuperMassive.Logging.AzureTable" -Build -IncludeReferencedProjects -Prop Configuration=Release -Symbols
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

# SuperMassive

![SuperMassive Logo](https://github.com/PulsarBlow/SuperMassive/blob/master/supermassive.png)

SuperMassive is a small condensed framework of reusable .NET components and utility classes.

### Version

Current : 1.3.0
Current : 1.4.2

### Projects descriptions

* **SuperMassive** : Core features - SuperMassive swiss army knife!
* **SuperMassive.ExceptionHandling** : Exception handling as it should be.
* **SuperMassive.ExceptionHandling.Logging** : Exception handling with logging features.
* **SuperMassive.Fakers** : A suit of data fakers to feed your hungry unit tests with "almost" real data.
* **SuperMassive.Logging** : A set of abstract and base logging components
* **SuperMassive.Logging.AzureTable** : A concrete implementation of the logging facade for Azure Table
Expand All @@ -22,6 +26,8 @@ Pick the features you want by installing the corresponding NuGet package :


```Ìnstall-Package SuperMassive```
```Ìnstall-Package SuperMassive.ExceptionHandling```
```Ìnstall-Package SuperMassive.ExceptionHandling.Logging```
```Ìnstall-Package SuperMassive.Fakers```
```Ìnstall-Package SuperMassive.Logging```
```Ìnstall-Package SuperMassive.Logging.AzureTable```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using SuperMassive.ExceptionHandling.Logging.Properties;
using SuperMassive.Logging;
using System;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;

namespace SuperMassive.ExceptionHandling.Logging
{
/// <summary>
/// Represents an <see cref="IExceptionHandler"/> that formats the exception into a log message
/// </summary>
public class LoggingExceptionHandler : IExceptionHandler
{
#region Members
private readonly string applicationName;
private readonly string logCategory;
private readonly int eventId;
private readonly TraceEventType severity;
private readonly string defaultTitle;
private readonly Type formatterType;
private readonly int minimumPriority;
private readonly ILogWriter logWriter;
#endregion

/// <summary>
/// Handler name
/// </summary>
public string Name
{
get { return this.GetType().Name; }
}


/// <summary>
/// Initializes a new instance of the <see cref="LoggingExceptionHandler"/> class with the log category, the event ID, the <see cref="TraceEventType"/>,
/// the title, minimum priority, the formatter type, and the <see cref="LogWriter"/>.
/// </summary>
/// <param name="applicationName"></param>
/// <param name="logCategory">The default category</param>
/// <param name="eventId">An event id.</param>
/// <param name="severity">The severity.</param>
/// <param name="title">The log title.</param>
/// <param name="priority">The minimum priority.</param>
/// <param name="formatterType">The type <see cref="ExceptionFormatter"/> type.</param>
/// <param name="writer">The <see cref="LogWriter"/> to use.</param>
/// <remarks>
/// The type specified for the <paramref name="formatterType"/> attribute must have a public constructor with
/// parameters of type <see name="TextWriter"/>, <see cref="Exception"/> and <see cref="Guid"/>.
/// </remarks>
public LoggingExceptionHandler(
string applicationName,
string logCategory,
int eventId,
TraceEventType severity,
string title,
int priority,
Type formatterType,
ILogWriter writer)
{
this.applicationName = applicationName;
this.logCategory = logCategory;
this.eventId = eventId;
this.severity = severity;
this.defaultTitle = title;
this.minimumPriority = priority;
this.formatterType = formatterType;
this.logWriter = writer;
}

/// <summary>
/// <para>Handles the specified <see cref="Exception"/> object by formatting it and writing to the configured log.</para>
/// </summary>
/// <param name="exception"><para>The exception to handle.</para></param>
/// <returns><para>Modified exception to pass to the next handler in the chain.</para></returns>
public Exception HandleException(Exception exception)
{
WriteToLog(CreateMessage(exception), exception.Data);
return exception;
}

/// <summary>
/// Writes the specified log message using the Logging Application Block's
/// method.
/// </summary>
/// <param name="logMessage">The message to write to the log.</param>
/// <param name="exceptionData">The exception's data.</param>
protected virtual void WriteToLog(string logMessage, IDictionary exceptionData)
{
LogEntry entry = new LogEntry(this.applicationName, logMessage, logCategory, minimumPriority, eventId, severity, defaultTitle, null);

foreach (DictionaryEntry dataEntry in exceptionData)
{
if (dataEntry.Key is string)
{
entry.ExtendedProperties.Add(dataEntry.Key as string, dataEntry.Value);
}
}

this.logWriter.Write(entry);
}

/// <summary>
/// Creates an instance of the <see cref="StringWriter"/>
/// class using its default constructor.
/// </summary>
/// <returns>A newly created <see cref="StringWriter"/></returns>
protected virtual StringWriter CreateStringWriter()
{
return new StringWriter(CultureInfo.InvariantCulture);
}

/// <summary>
/// Creates an <see cref="ExceptionFormatter"/>
/// object based on the configured ExceptionFormatter
/// type name.
/// </summary>
/// <param name="writer">The stream to write to.</param>
/// <param name="exception">The <see cref="Exception"/> to pass into the formatter.</param>
/// <returns>A newly created <see cref="ExceptionFormatter"/></returns>
protected virtual ExceptionFormatter CreateFormatter(
StringWriter writer,
Exception exception)
{
ConstructorInfo constructor = GetFormatterConstructor();
return (ExceptionFormatter)constructor.Invoke(
new object[] { writer, exception }
);
}

private ConstructorInfo GetFormatterConstructor()
{
Type[] types = new Type[] { typeof(TextWriter), typeof(Exception) };
ConstructorInfo constructor = formatterType.GetConstructor(types);
if (constructor == null)
{
throw new ExceptionHandlingException(
string.Format(CultureInfo.CurrentCulture, Resources.MissingConstructor, formatterType.AssemblyQualifiedName));
}
return constructor;
}

private string CreateMessage(Exception exception)
{
StringWriter writer = null;
StringBuilder stringBuilder = null;
try
{
writer = CreateStringWriter();
ExceptionFormatter formatter = CreateFormatter(writer, exception);
formatter.Format();
stringBuilder = writer.GetStringBuilder();

}
finally
{
if (writer != null)
{
writer.Close();
}
}

return stringBuilder.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("SuperMassive.ExceptionHandling.Logging")]
[assembly: AssemblyDescription("Exception Handling with logging")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("f65e6698-393e-45e3-82bf-95950ae55ae8")]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 05c06a8

Please sign in to comment.