Logger: Log application exceptions
This commit is contained in:
116
VAR.Toolbox/Code/Logger.cs
Normal file
116
VAR.Toolbox/Code/Logger.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace VAR.Toolbox.Code
|
||||
{
|
||||
public class Logger
|
||||
{
|
||||
/// <summary>
|
||||
/// Obtiene el StreamWritter de salida
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static StreamWriter GetOutputStreamWritter()
|
||||
{
|
||||
try
|
||||
{
|
||||
string location = System.Reflection.Assembly.GetEntryAssembly().Location;
|
||||
string path = Path.GetDirectoryName(location);
|
||||
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(location);
|
||||
|
||||
string fileOut = String.Format("{0}/{1}.{2}.txt", path, filenameWithoutExtension,
|
||||
DateTime.UtcNow.ToString("yyyy-MM"));
|
||||
return File.AppendText(fileOut);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cierra el StreamWritter de salida
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream.</param>
|
||||
private static void CloseOutputStreamWritter(StreamWriter stream)
|
||||
{
|
||||
if (stream != null)
|
||||
{
|
||||
stream.Close();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Escribe una linea. En el stream y en la consola
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream.</param>
|
||||
/// <param name="line">The line.</param>
|
||||
private static void WriteLine(StreamWriter stream, string line)
|
||||
{
|
||||
if (stream != null)
|
||||
{
|
||||
stream.WriteLine(line);
|
||||
}
|
||||
Console.Out.WriteLine(line);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logea el marcador
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
public static void Marker(String text)
|
||||
{
|
||||
try
|
||||
{
|
||||
StreamWriter outStream = GetOutputStreamWritter();
|
||||
WriteLine(outStream, string.Empty);
|
||||
WriteLine(outStream, String.Format("---------------------------- {0} -----------------------", text));
|
||||
WriteLine(outStream, String.Format("\\- Date: {0}", DateTime.UtcNow.ToString("s")));
|
||||
WriteLine(outStream, string.Empty);
|
||||
CloseOutputStreamWritter(outStream);
|
||||
}
|
||||
catch (Exception) { /* Nom Nom Nom */}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logea el texto especificado
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
public static void Log(String text)
|
||||
{
|
||||
try
|
||||
{
|
||||
StreamWriter outStream = GetOutputStreamWritter();
|
||||
WriteLine(outStream, String.Format("{0} -- {1}", DateTime.UtcNow.ToString("s"), text));
|
||||
CloseOutputStreamWritter(outStream);
|
||||
}
|
||||
catch (Exception) { /* Nom Nom Nom */}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Logea una excepcion
|
||||
/// </summary>
|
||||
/// <param name="ex">The Exception.</param>
|
||||
public static void Log(Exception ex)
|
||||
{
|
||||
try
|
||||
{
|
||||
StreamWriter outStream = GetOutputStreamWritter();
|
||||
WriteLine(outStream, string.Empty);
|
||||
WriteLine(outStream, String.Format("!!!!!!!!!!!!!!!!!!!!!!!!!!!! {0} !!!!!!!!!!!!!!!!!!!!!!!", "Exception"));
|
||||
WriteLine(outStream, String.Format("\\- Date: {0}", DateTime.UtcNow.ToString("s")));
|
||||
WriteLine(outStream, string.Empty);
|
||||
Exception exAux = ex;
|
||||
while (exAux != null)
|
||||
{
|
||||
WriteLine(outStream, String.Format("Message: {0}", exAux.Message));
|
||||
WriteLine(outStream, String.Format("Stacktrace: {0}", exAux.StackTrace));
|
||||
exAux = exAux.InnerException;
|
||||
}
|
||||
CloseOutputStreamWritter(outStream);
|
||||
}
|
||||
catch (Exception) { /* Nom Nom Nom */}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using VAR.Toolbox.UI;
|
||||
using VAR.Toolbox.Code;
|
||||
|
||||
namespace VAR.Toolbox
|
||||
{
|
||||
@@ -12,9 +13,25 @@ namespace VAR.Toolbox
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
Application.ThreadException += Application_ThreadException;
|
||||
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.Run(new FrmToolbox());
|
||||
try
|
||||
{
|
||||
Application.Run(new FrmToolbox());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log(ex);
|
||||
Application.Exit();
|
||||
}
|
||||
}
|
||||
|
||||
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
|
||||
{
|
||||
Logger.Log(e.Exception);
|
||||
Application.Exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,6 +80,7 @@
|
||||
<Compile Include="Code\DirectShow\Tools.cs" />
|
||||
<Compile Include="Code\DirectShow\Uuids.cs" />
|
||||
<Compile Include="Code\DirectShow\Win32.cs" />
|
||||
<Compile Include="Code\Logger.cs" />
|
||||
<Compile Include="Code\Mouse.cs" />
|
||||
<Compile Include="Code\User32.cs" />
|
||||
<Compile Include="Code\Webcam.cs" />
|
||||
|
||||
Reference in New Issue
Block a user