zoukankan      html  css  js  c++  java
  • AppDiag类

    AppDiag
    #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
    }
    }
    }
    }
    }
  • 相关阅读:
    vue学习(1)
    10.贝叶斯理论
    9.高等数学-导数
    5.6.7.8.高等数学-两个重要的极限定理
    4. 高等数学——元素和极限
    1.2.3.《万门大学-人工智能、大数据、复杂系统》课程大纲
    1.0初识机器学习
    django2+uwsgi+nginx上线部署到服务器Ubuntu16.04(最新最详细版)
    ERRORS: ?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.
    Ubuntu下MySQL报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  • 原文地址:https://www.cnblogs.com/Googler/p/2005935.html
Copyright © 2011-2022 走看看