Exemple #1
0
        /// <summary>
        /// Error事件处理方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void App_Error(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            Exception       ex  = app.Server.GetLastError();

            if (ex == null)
            {
                return;
            }

            if (IsNeedLog(ex) == false)
            {
                return;
            }

            DbCommand dbCommand = null;

            IIncludeDbCommand dbExceuteException = ex as IIncludeDbCommand;

            if (dbExceuteException != null)
            {
                dbCommand = dbExceuteException.Command;
            }

            ExceptionInfo exceptionInfo = ExceptionInfo.Create(ex, app.Context, dbCommand);

            LogHelper.Write(exceptionInfo);
        }
        private List <ExceptionInfo> WriteList()
        {
            Exception     ex1            = LogHelperTest.CreateException("M1-" + Guid.NewGuid().ToString());
            ExceptionInfo exceptionInfo1 = ExceptionInfo.Create(ex1);

            exceptionInfo1.Addition = "If you liked Fiddler, we also recommend";
            //exceptionInfo1.A1 = "aaaaaaaaaaaa";


            System.Threading.Thread.Sleep(1000);


            Exception     ex2            = LogHelperTest.CreateException("M2-" + Guid.NewGuid().ToString());
            ExceptionInfo exceptionInfo2 = ExceptionInfo.Create(ex2);

            exceptionInfo2.Addition = "Test Studio - Create Automated Tests Quickly";
            //exceptionInfo2.A1 = "aaaaaaaaaaaa";


            MongoDbWriter        writer = new MongoDbWriter();
            List <ExceptionInfo> list1  = new List <ExceptionInfo> {
                exceptionInfo1, exceptionInfo2
            };

            writer.Write(list1);

            System.Threading.Thread.Sleep(1000);

            return(list1);
        }
Exemple #3
0
        public void Test2()
        {
            // 测试有上传文件时,获取表单数据的逻辑

            using (WebContext context = CreateWebContext()) {
                context.SetUserName("Fish Li");


                Type            httpFileCollectionType = typeof(HttpContext).Assembly.GetType("System.Web.HttpFileCollection");
                ConstructorInfo ctor = httpFileCollectionType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
                var             httpFileCollection = ctor.Invoke(null);

                MethodInfo addFileMethod = httpFileCollectionType.GetMethod("AddFile", BindingFlags.Instance | BindingFlags.NonPublic);
                addFileMethod.Invoke(httpFileCollection, new object[] { "file1", null });

                FieldInfo field = typeof(HttpRequest).GetField("_files", BindingFlags.Instance | BindingFlags.NonPublic);
                field.SetValue(context.HttpContext.Request, httpFileCollection);


                context.Request.SetForm("a=1&b=2");


                Exception     ex   = LogHelperTest.CreateException("Test: HttpInfo.Create");
                ExceptionInfo info = ExceptionInfo.Create(ex, context.HttpContext, null);
                info.Addition = Guid.NewGuid().ToString();

                LogHelper.SyncWrite(info);

                LogHelperTest.AssertWriteOK(info.Addition);
            }
        }
        public void Test2()
        {
            // 测试 s_initOK 的写入分支,检验方法就是查看代码覆盖率

            FieldInfo field        = typeof(WinLogWriter).GetField("s_initOK", BindingFlags.Static | BindingFlags.NonPublic);
            bool      currentValue = (bool)field.GetValue(null);

            field.SetValue(null, false);

            try {
                Exception ex = LogHelperTest.CreateException();
                //("TestMessage: 顺序拟安排如下:集成专项、建模专项、越秀专项、售楼+公共专项 ");
                ExceptionInfo exInfo = ExceptionInfo.Create(ex);


                WinLogWriter writer = new WinLogWriter();
                writer.Write(exInfo);


                List <ExceptionInfo> list = new List <ExceptionInfo> {
                    exInfo
                };
                writer.Write(list);

                // 不抛出异常就算是通过,所以没有断言
            }
            finally {
                field.SetValue(null, currentValue);
            }
        }
        public List <CodeCheckResult> Execute(string filePath)
        {
            List <MethodCheckResult> list = new List <MethodCheckResult>();

            try {
                List <MethodCodeInfo> methods = ParseMethods(filePath);
                list.AddRange(CheckMethod(methods));
            }
            catch (Exception ex) {
                ExceptionInfo exceptionInfo = ExceptionInfo.Create(ex);
                ClownFish.Log.LogHelper.SyncWrite(exceptionInfo);

                // 这里吃掉异常是因为:没有必要因为一个文件扫描失败而结束整个扫描过程。
            }


            List <CodeCheckResult> results = new List <CodeCheckResult>();

            foreach (var mc in list)
            {
                results.Add(new CodeCheckResult {
                    FileName = mc.FilePath,
                    LineNo   = mc.Method.LineNo,
                    LineText = mc.Method.MethodName,
                    Reason   = mc.Reason
                });
            }

            return(results);
        }
Exemple #6
0
 private static ExceptionInfo CreatExceptionInfo(ExceptionDto dto)
 {
     return(dto == null
         ? null
         : ExceptionInfo.Create(dto.Message, dto.Source,
                                dto.StackTrace, CreatExceptionInfo(dto.InnerException)));
 }
        public void ProcessException(Exception ex)
        {
            ExceptionInfo exceptionInfo = ExceptionInfo.Create(ex);

            ClownFish.Log.LogHelper.SyncWrite(exceptionInfo);

            ConsoleWrite(ex.ToString());
        }
Exemple #8
0
        public void Test_LogHelper_SyncWrite()
        {
            Exception     ex     = CreateException();
            ExceptionInfo exInfo = ExceptionInfo.Create(ex);

            LogHelper.SyncWrite(exInfo);

            AssertWriteOK(ex.Message);
        }
        /// <summary>
        /// 将一个异常对象通过 ClownFish.Log 记录下来,
        /// 如果还需要记录HTTP请求或者SQL执行信息,请先调用ExceptionInfo.Create()
        /// </summary>
        /// <param name="ex">Exception实例</param>
        /// <param name="syncWrite">是否采用同步方式写入日志</param>
        public static void SaveToLog(this Exception ex, bool syncWrite = false)
        {
            if (ex == null)
            {
                throw new ArgumentException(nameof(ex));
            }

            ExceptionInfo exceptionInfo = ExceptionInfo.Create(ex);

            SaveToLog(exceptionInfo);
        }
Exemple #10
0
        private void App_Error(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            Exception       ex  = app.Server.GetLastError();

            if (ex != null)
            {
                ExceptionInfo exceptionInfo = ExceptionInfo.Create(ex);
                ClownFish.Log.LogHelper.SyncWrite(exceptionInfo);
            }
        }
Exemple #11
0
        public void Test_LogHelper_AsyncWrite()
        {
            Exception     ex     = CreateException(Guid.NewGuid().ToString());
            ExceptionInfo exInfo = ExceptionInfo.Create(ex);

            LogHelper.Write(exInfo);

            // 等待定时器启动
            System.Threading.Thread.Sleep(1000);

            AssertWriteOK(ex.Message);
        }
Exemple #12
0
        static void ProcessException(Exception ex)
        {
            ExceptionInfo exceptionInfo = ExceptionInfo.Create(ex);

            ClownFish.Log.LogHelper.SyncWrite(exceptionInfo);


            if (s_enableConsoleOut)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Exemple #13
0
        private void Test(int a, int b)
        {
            try {
                if (b < 0)
                {
                    throw new NotImplementedException();
                }

                int c = a / b;
            }
            catch (Exception ex) {
                ExceptionInfo exInfo = ExceptionInfo.Create(ex);
                LogHelper.Write(exInfo);
            }

            System.Threading.Thread.Sleep(1000);
        }
Exemple #14
0
        /// <summary>
        /// Error事件处理方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void App_Error(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            Exception       ex  = app.Server.GetLastError();

            if (ex != null)
            {
                DbCommand dbCommand = null;

                DbExceuteException dbExceuteException = ex as DbExceuteException;
                if (dbExceuteException != null)
                {
                    dbCommand = dbExceuteException.Command;
                }

                ExceptionInfo exceptionInfo = ExceptionInfo.Create(ex, app.Context, dbCommand);
                LogHelper.Write(exceptionInfo);
            }
        }
        public void Test_Write()
        {
            Exception     ex             = LogHelperTest.CreateException("M-" + Guid.NewGuid().ToString());
            ExceptionInfo exceptionInfo1 = ExceptionInfo.Create(ex);

            exceptionInfo1.Addition = "If you liked Fiddler, we also recommend";

            MongoDbWriter writer = new MongoDbWriter();

            writer.Write(exceptionInfo1);


            ExceptionInfo exceptionInfo2 = writer.Get <ExceptionInfo>(exceptionInfo1.InfoGuid);

            Assert.AreEqual(exceptionInfo1.Message.ToString(), exceptionInfo2.Message.ToString());
            Assert.AreEqual(exceptionInfo1.Addition.ToString(), exceptionInfo2.Addition.ToString());

            writer.Delete <ExceptionInfo>(exceptionInfo1.InfoGuid);
        }
Exemple #16
0
        /// <summary>
        /// Error事件处理方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void App_Error(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            Exception       ex  = app.Server.GetLastError();

            if (ex != null)
            {
                DbCommand dbCommand = null;
                Type      t         = ex.GetType();
                if (t.Namespace == "ClownFish.Data" && t.Name == "DbExceuteException")
                {
                    PropertyInfo p = t.GetProperty("Command");
                    dbCommand = (DbCommand)p.GetValue(ex);
                }

                ExceptionInfo exceptionInfo = ExceptionInfo.Create(ex, app.Context, dbCommand);
                ClownFish.Log.LogHelper.Write(exceptionInfo);
            }
        }
        public void Test1()
        {
            // 正常情况测试

            Exception     ex     = LogHelperTest.CreateException();
            ExceptionInfo exInfo = ExceptionInfo.Create(ex);

            WinLogWriter writer = new WinLogWriter();

            writer.Write(exInfo);



            List <ExceptionInfo> list = new List <ExceptionInfo> {
                exInfo
            };

            writer.Write(list);

            // 不抛出异常就算是通过,所以没有断言
        }
Exemple #18
0
        public async Task Invoke(HttpContext context, ILogger <JsonExceptionMiddleware> logger)
        {
            var exception = context.Features.Get <IExceptionHandlerFeature>()?.Error;

            if (exception == null)
            {
                await Next(context);

                return;
            }

            logger.LogError(exception, "Unhandled exception in {Method} {RequestPath}: {ExceptionType} - {ExceptionMessage}",
                            context.Request.Method, context.Request.Path, exception.GetType().Name, exception.Message);

            var result = ExceptionInfo.Create(exception, !Environment.IsProduction());
            var json   = JsonConvert.SerializeObject(result, SerializerSettings);

            context.Response.StatusCode  = StatusCodes.Status500InternalServerError;
            context.Response.ContentType = "application/json; charset=utf-8";
            await context.Response.WriteAsync(json, Encoding.UTF8);
        }
Exemple #19
0
        public void Test_ExceptionInfo_Addition()
        {
            // 测试普通情况

            using (WebContext context = CreateWebContext()) {
                context.SetUserName("Fish Li");
                context.AddSession("session-1", "aaaaaaaaaaaa");
                context.AddSession("session-2", DateTime.Now);
                context.AddSession("session-3", null);

                context.Request.SetInputStream("a=1&b=2");


                Exception     ex   = LogHelperTest.CreateException("Test: HttpInfo.Create");
                ExceptionInfo info = ExceptionInfo.Create(ex, context.HttpContext, null);
                info.Addition = Guid.NewGuid().ToString();

                LogHelper.SyncWrite(info);

                LogHelperTest.AssertWriteOK(info.Addition);
            }
        }
Exemple #20
0
        public void Test_LogHelper_Write_DbCommand()
        {
            using (WebContext context = HttpInfoTest.CreateWebContext()) {
                context.SetUserName("Fish Li");
                context.AddSession("session-1", "aaaaaaaaaaaa");
                context.AddSession("session-2", DateTime.Now);
                context.AddSession("session-3", null);
                context.Request.SetInputStream("a=1&b=2");

                DbCommand command = SqlInfoTest.CreateDbCommand();


                Exception     ex   = CreateException("Test: HttpInfo.Create");
                ExceptionInfo info = ExceptionInfo.Create(ex, context.HttpContext, command);
                info.Addition = Guid.NewGuid().ToString();

                LogHelper.SyncWrite(info);

                LogHelperTest.AssertWriteOK(info.Addition);

                AssertWriteOK(ex.Message);
            }
        }
Exemple #21
0
 private void ProcessFlushException(List <Exception> exceptions)
 {
     if (exceptions.Count > 0)
     {
         ILogWriter retryLogger = WriterFactory.GetRetryWriter();                                // 获取重试日志序列化器
         if (retryLogger != null)
         {
             try {
                 List <ExceptionInfo> list = (from x in exceptions select ExceptionInfo.Create(x)).ToList();
                 retryLogger.Write(list);
             }
             catch (Exception ex) {
                 LogHelper.RaiseErrorEvent(ex);
             }
         }
         else
         {
             foreach (Exception ex in exceptions)
             {
                 LogHelper.RaiseErrorEvent(ex);
             }
         }
     }
 }
 public void Test_Write()
 {
     ExceptionInfo info = ExceptionInfo.Create(null, null, null);
 }
Exemple #23
0
 public void Test_ExceptionInfo_Create_Argument_null()
 {
     ExceptionInfo info = ExceptionInfo.Create(null, null, null);
 }
        /// <summary>
        /// 解析一个C#文件中的方法定义
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private List <MethodCodeInfo> ParseMethods(string file)
        {
            if (File.Exists(file) == false)
            {
                throw new FileNotFoundException("文件不存在:" + file);
            }


            string     text = File.ReadAllText(file);
            SyntaxTree tree = CSharpSyntaxTree.ParseText(text);
            var        root = (CompilationUnitSyntax)tree.GetRoot();

            List <MethodCodeInfo> list = new List <MethodCodeInfo>();

            for (int i = 0; i < root.Members.Count; i++)
            {
                NamespaceDeclarationSyntax ns = root.Members[i] as NamespaceDeclarationSyntax;
                if (ns == null)
                {
                    break;
                }

                for (int j = 0; j < ns.Members.Count; j++)
                {
                    ClassDeclarationSyntax cls = ns.Members[j] as ClassDeclarationSyntax;
                    if (cls == null)
                    {
                        continue;
                    }

                    for (int m = 0; m < cls.Members.Count; m++)
                    {
                        MethodDeclarationSyntax method = cls.Members[m] as MethodDeclarationSyntax;
                        if (method == null)
                        {
                            continue;
                        }

                        if (method.Body == null)                                // abstract
                        {
                            continue;
                        }

                        try {
                            list.Add(new MethodCodeInfo {
                                FilePath   = file,
                                NameSpace  = ns.Name.ToString(),
                                ClassName  = cls.Identifier.Text,
                                MethodName = method.Identifier.Text,
                                Body       = method.Body.ToString(),
                                LineNo     = method.SyntaxTree.GetMappedLineSpan(method.Span).StartLinePosition.Line
                            });
                        }
                        catch (Exception ex) {
                            ExceptionInfo exceptionInfo = ExceptionInfo.Create(ex);
                            ClownFish.Log.LogHelper.SyncWrite(exceptionInfo);

                            // 这里吃掉异常是因为:没有必要因为一个文件扫描失败而结束整个扫描过程。
                        }
                    }
                }
            }

            return(list);
        }