MVC Json输出调试信息

当测试环境、生产环境不能够调试的时候,有的时候想通过一些参数跟踪出想要的数据。如:SOA,DALSQL 等。

 
public class DebugJsonResult: JsonResult
    {
        public DebugJsonResult(object Data)
        {
            base.Data = Data;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            HttpResponseBase response = context.HttpContext.Response;

            if (!string.IsNullOrEmpty(ContentType))
                response.ContentType = ContentType;
            else
                response.ContentType = "application/json";

            if (ContentEncoding != null)
                response.ContentEncoding = ContentEncoding;

            if (Data != null)
            {
                string jsonData = JsonNetHelper.ToJson(Data);
                if (ControllerDebug.GetInstance.IsDebug == true)
                {
                    try
                    {
                        if (jsonData.LastIndexOf("}") + 1 == jsonData.Length)
                        {
                            DebugInfoResult debuginfo = new DebugInfoResult();
                            debuginfo.DebugInfo = ControllerDebug.GetInstance.DebugList;

                            string strdebuginfo = jsonData.Insert(jsonData.LastIndexOf("}"), "," + JsonNetHelper.ToJson(ControllerDebug.GetInstance.DebugList));
                            response.Write(strdebuginfo);
                        }
                        else
                        {
                            response.Write(jsonData);
                        }
                    }
                    catch
                    {

                        response.Write(jsonData);
                    }
                }
                else
                {

                    response.Write(jsonData);
                }
            }
        }
    }

    public class DebugInfoResult
    {
        /// <summary>
        /// Debug 信息
        /// </summary>
        public object DebugInfo
        {
            get;
            set;
        }
    }

在 调用Controller 是传入isdebug 参数.

public class IoCControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
if (HttpContext.Current.Request.QueryString["isdebug"]!=null)
            {
                string sisdebug = HttpContext.Current.Request.QueryString["isdebug"].ToString().ToUpper();

                if (sisdebug.IndexOf("T") >= 0)
                {
                    string tempisdebug = sisdebug.Replace("T","").Replace("-","");

                    if (!string.IsNullOrEmpty(tempisdebug))
                    {
                         ControllerDebug.GetInstance.SearchType= (EnumDebug)Enum.Parse(typeof(EnumDebug), tempisdebug.ToUpper());
                    }

                    //开启调试.
                    ControllerDebug.GetInstance.IsDebug = true;
                    ControllerDebug.GetInstance.DebugList.Clear();
                }
                else
                {
                    ControllerDebug.GetInstance.IsDebug = false;
                    ControllerDebug.GetInstance.DebugList.Clear();
                }
            }
            else
            {
                ControllerDebug.GetInstance.IsDebug = false;
                ControllerDebug.GetInstance.DebugList.Clear();
            }
return base.GetControllerInstance(requestContext, controllerType);
     }
}
public abstract class BaseController : System.Web.Mvc.Controller
    {
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding)
        {
            return new DebugJsonResult(data);
        }
     }
public class BookingGroupOrderController : BaseController
    {
    [HttpPost]
        public ActionResult CreateOrder(O_BookingCreateOrder obj)
        {
            IOrderBiz GOBiz = Ioc.IoCFactory.Instance.CurrentContainer.Resolve<IOrderBiz>("BookingGroupOrder");

            ICreateOrder createobject = GOBiz.CreateOrder<O_BookingCreateOrder>(obj);

            return Json(createobject, null, null);
        }     }

时间: 2024-08-11 01:27:03

MVC Json输出调试信息的相关文章

跟踪Makefile输出调试信息

/********************************************************************* * 跟踪Makefile输出调试信息 * 说明: * 有时候为了了解项目的Makefile是怎么工作的,需要加入一些调试信息, * 查看一些变量状态,来判断编译流程是如何进行的. * * 2017-8-14 深圳 龙华樟坑村 曾剑锋 **************************************************************

C/C++ __FILE__,__LINE__输出调试信息

在写程序的时候,总是或多或少会加入一些printf之类的语句用于输出调试信息,但是printf语句有个很不方便的地方就是当我们需要发布程序的时候要一条一条的把这些语句删除,而一旦需要再次调试的时候,这些语句又不得不一条条的加上,这给我们带来了很大的不便,浪费了我们很多的时间,也造成了调试的效率低下.所以,很多人会选择使用宏定义的方式来输出调试语句. 编译器内置宏,ANSI C标准中有几个标准预定义宏(也是常用的): __LINE__:    在源代码中插入当前源代码行号: __FILE__:  

【OT1.0 + TP3.2】开启trace调试、输出调试信息、开启自定义菜单

1.开启trace调试 A- 后台系统设置 show-page-trace = 1 B-config.php文件.配置 show-page-trace = true 2.输出调试信息 很奇怪,OT竟然把 dump.var_dump 都被干掉了,不让用,很是纠结,然后我们可以使用下面方法进行调试: //调试 文件位置\Runtime\Logs\Admin \Think\Log::record('Team->del: '.json_encode($_POST,JSON_UNESCAPED_UNICO

C语言使用宏输出调试信息实战

如何使用宏定义输出信息 C语言提供了#, ##, __VA_ARGS__等符号来帮助我们在宏定义中更好地输出信息.使用方式如下: 输出如下: #的作用是,它会在所引用的宏变量左右两边加一对引号.所以FUN1中的printf函数不会出错,因为FUN1(hello)预编译时被替换成了printf("hello\n") ##的作用是,把两个宏变量连接成一个,FUN2(1, 2)预编译时被替换成了printf("The num is %d\n", 12) 支持c99标准的编

[Android Pro] Android studio jni中调用Log输出调试信息

reference to : http://www.linuxidc.com/Linux/2014-02/96341.htm Android 开发中,java 可以方便的使用调试信息Log.i, Log.d ...,Jni层可否使用呢?答案是肯定的. 1 为方便使用,先进行宏定义: eben_hpc_log.h内容如下: #ifndef _Included_hpc_Log#define _Included_hpc_Log#ifdef __cplusplusextern "C" {#en

QT5入门之12 - QDebug输出调试信息

这个很简单,二步即可. 1.添加头文件 #include <qdebug.h> 2.输出信息 qDebug("Test:%d",id); (%d表示整数) 3.格式化信息 %c 读入一个字符  %d 读入十进制整数  %x,%X 读入十六进制整数  %s 读入一个字符串,遇空格.制表符或换行符结束.  %f,%F 用来输入实数,可以用小数形式或指数形式输入. 3.输出在应用程序输出中可见.release不需删除调试代码. 4.其他输出. qWarning():输出警告信息 

vs调试时底部输出调试信息“无法查找或打开 PDB 文件”解决办法

用VS调试程序时,有时会在VS底部的“输出”框中提示“无法查找或打开 PDB 文件”.这该怎么解决呢? 下面,我们以VS2013为例,来教大家解决办法. 工具/原料 VS 方法/步骤 打开VS2013,点击菜单“工具”-“选项”.   在选项窗口中,展开“调试”-“常规”,然后在右边的窗格中勾选“启用源服务器支持”.   然后展开“调试”-“符号”,勾选“Windows符号服务器”.   这时,会弹出一个警告对话框,无视点击“确定”即可.   最后,点击“确定”关闭选项窗口.   下面,我们再来

Android真机调试的时候logcat中无法输出调试信息的解决办法

真机调试不输出日志到logcat的原因是手机厂商默认关闭了调试打印的功能,通过以下方法开启此方法. 下面以华为P6手机为例进行操作: 1.在拨号界面输入:*#*#2846579#*#* 进入测试菜单界面. 2.工程菜单–后台设置–LOG设置 3.LOG开关–LOG打开 OK至此设置完毕,手机会自动重启以下,然后就可以用了 对于别的手机可能还会需要以下两步: 4.LOG级别设置–VERBOSE 5.Dump&Log– 全部选中

IAR 下 printf输出调试信息 Terminal I/O

测试环境 XP  +IAR 5.4+JLink v8 IAR下的设置 有些人说勾选 Via SWO,我测试不行. 还有些人说中选中SWO SETTING,enable相应的PROT,经过测试,打不开没有影响输出 调用代码 显示