AppDiag类

ContractedBlock.gifExpandedBlockStart.gifAppDiag
#define TRACE
//#undef TRACE
#define PerfMonitor
using System;
using System.Diagnostics;
using System.Reflection;
using System.Web;
using System.Threading;

namespace Rocky
{
public static class AppDiag
{
public const string DebugSymbal = "DEBUG";
#if TRACE
/// <summary>
/// Trace Enhanced tracing functionality to consolidate system and http tracing log and provide support for trace switches.
/// </summary>
private static readonly TraceSwitch traceSwitch = new TraceSwitch("AppTrace", "Runtime Trace");
#endif

[Conditional(DebugSymbal)]
public static void GuardArgument(object arg)
{
GuardArgument(arg,
string.Empty);
}
[Conditional(DebugSymbal)]
public static void GuardArgument(object arg, string paramName)
{
if (arg == null)
{
try
{
System.Diagnostics.Debug.Assert(
false, paramName);
#if TRACE
System.Diagnostics.Trace.Assert(
false, paramName);
#endif
}
catch
{

}
throw new ArgumentNullException(paramName);
}
}

[Conditional(DebugSymbal)]
public static void GuardArgument(bool ifTrue)
{
GuardArgument(ifTrue,
string.Empty);
}
[Conditional(DebugSymbal)]
public static void GuardArgument(bool ifTrue, string message)
{
if (ifTrue)
{
try
{
System.Diagnostics.Debug.Assert(
false, message);
#if TRACE
System.Diagnostics.Trace.Assert(
false, message);
#endif
}
catch
{

}
throw new ArgumentException(message);
}
}

[Conditional(DebugSymbal)]
public static void Guard<T>(bool condition) where T : Exception
{
Guard
<T>(condition, string.Empty);
}
[Conditional(DebugSymbal)]
public static void Guard<T>(bool condition, string message) where T : Exception
{
if (condition)
{
try
{
System.Diagnostics.Debug.Assert(
false, message);
#if TRACE
System.Diagnostics.Trace.Assert(
false, message);
#endif
}
catch
{

}
throw (T)Activator.CreateInstance(typeof(T), message);
}
}

[Conditional(DebugSymbal)]
public static void Guard<T>(Func<bool> predicate) where T : Exception
{
Guard
<T>(predicate, string.Empty);
}
[Conditional(DebugSymbal)]
public static void Guard<T>(Func<bool> predicate, string message) where T : Exception
{
if (predicate())
{
try
{
System.Diagnostics.Debug.Assert(
false, message);
#if TRACE
System.Diagnostics.Trace.Assert(
false, message);
#endif
}
catch
{

}
throw (T)Activator.CreateInstance(typeof(T), message);
}
}

[Conditional(DebugSymbal)]
public static void Trace(string message)
{
Trace(TraceLevel.Info, message);
}
[Conditional(DebugSymbal)]
public static void Trace(TraceLevel level, string message)
{
if (level <= traceSwitch.Level)
{
try
{
System.Diagnostics.Trace.WriteLine(message);
HttpContext httpContext
= HttpContext.Current;
if (httpContext != null)
{
if (level == TraceLevel.Error)
{
httpContext.Trace.Warn(message);
}
else
{
httpContext.Trace.Write(message);
}
}
}
catch
{
// Do nothing: do not corrupt the current error with a failure to trace an error
}
}
}
}
}

转载于:https://www.cnblogs.com/Googler/archive/2011/04/05/2005935.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值