Skip to content

Instantly share code, notes, and snippets.

@Segment0895
Last active June 7, 2016 11:27
Show Gist options
  • Save Segment0895/ab59889636f8ac4333be51ffb5321999 to your computer and use it in GitHub Desktop.
Save Segment0895/ab59889636f8ac4333be51ffb5321999 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using log4net;
namespace DesignByContractProgramming
{
public class Excepcoes6
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static Random rd = new Random(new object().GetHashCode());
private static bool TCRAutoDesligado = Properties.Settings.Default.testes2;
public static bool TCRAuto(Action FuncaoDefinidaExteriormente, string idStrOperacao2 = @"", [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
{
return TCTemplate(FuncaoDefinidaExteriormente, 10, idStrOperacao2, memberName, filePath, lineNumber);
}
public static bool Precondicao(Action FuncaoDefinidaExteriormente, string idStrOperacao2 = @"", [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
{
return TCTemplate(FuncaoDefinidaExteriormente, 1, idStrOperacao2, memberName, filePath, lineNumber);
}
public static bool Poscondicao(Action FuncaoDefinidaExteriormente, string idStrOperacao2 = @"", [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
{
return TCTemplate(FuncaoDefinidaExteriormente, 1, idStrOperacao2, memberName, filePath, lineNumber);
}
public static bool TCOnce(Action FuncaoDefinidaExteriormente, string idStrOperacao2 = @"", [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
{
return TCTemplate(FuncaoDefinidaExteriormente, 1, idStrOperacao2, memberName, filePath, lineNumber);
}
public static bool TCTemplate(Action FuncaoDefinidaExteriormente, int numTentativasRest, string idStrOperacao2, string memberName, string filePath, int lineNumber)
{
bool sucesso = false;
int testesDividor = 1000;
if (TCRAutoDesligado)
{
FuncaoDefinidaExteriormente();
sucesso = true;
}
else
{
string idStrOperacao1 = Path.GetFileName(filePath) + "- " + memberName + "- " + lineNumber;
string idStrOperacao3 = "";
if (String.IsNullOrWhiteSpace(idStrOperacao2))
{
idStrOperacao3 = idStrOperacao1;
}
else
{
idStrOperacao3 = idStrOperacao1 + ", DESC: " + idStrOperacao2;
}
Exception ex2 = null;
while (sucesso == false && numTentativasRest-- > 0)
{
try
{
FuncaoDefinidaExteriormente();
sucesso = true;
}
catch (Exception ex1)
{
if (numTentativasRest > 0)
{
log.Warn("WARN: " + idStrOperacao3 + "falhou, a tentar mais " + numTentativasRest + "vezes.");
System.Threading.Thread.Sleep(rd.Next(1000 / testesDividor, 10000 / testesDividor));
}
else
{
ex2 = ex1;
}
}
}
if (sucesso == false)
{
log.Error(memberName + @":" + idStrOperacao3 + @" falhou: A DESISTIR");
if (ex2 == null)
{
log.Error(memberName + @":" + idStrOperacao3 + @" falhou mas sem excepção (??).");
}
else
{
log.Error(memberName + @":" + @"Data:");
log.Error(ex2.Data); log.Error(@":Data EOF");
log.Error(memberName + @":" + @"HResult:");
log.Error(ex2.HResult); log.Error(@":HResult EOF");
log.Error(memberName + @":" + @"InnerException:");
log.Error(ex2.InnerException); log.Error(@":InnerException EOF");
log.Error(memberName + @":" + @"Message:");
log.Error(ex2.Message); log.Error(@":Message EOF");
log.Error(memberName + @":" + @"Source:");
log.Error(ex2.Source); log.Error(@":Source EOF");
log.Error(memberName + @":" + @"StackTrace:");
log.Error(ex2.StackTrace); log.Error(@":StackTrace EOF");
log.Error(memberName + @":" + @"TargetSite:");
log.Error(ex2.TargetSite); log.Error(@":TargetSite EOF");
log.Error(memberName + @":" + @"ToString:");
log.Error(ex2.ToString()); log.Error(@":ToString EOF");
log.Error(@":" + memberName + " EOF");
}
}
}
return sucesso;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment