public void ExceptionIsSilentlySwallowed()
        {
            CommonTestHelpers.ResetContext();

            KissLogConfiguration.InternalLog = (message) =>
            {
                throw new Exception();
            };

            InternalLogger.LogException(new Exception());
        }
        public void LogException()
        {
            string messageArg = null;

            KissLogConfiguration.InternalLog = (message) => messageArg = message;

            string exceptionMessage = $"Exception {Guid.NewGuid()}";

            InternalLogger.LogException(new Exception(exceptionMessage));

            Assert.IsNotNull(messageArg);
            Assert.IsTrue(messageArg.Contains(exceptionMessage));
        }
示例#3
0
 // Simple handling here. If an extended class wants to do something special, override this method
 public virtual void HandleException(ITask task, Exception exception)
 {
     if (ExceptionHandler != null)
     {
         try
         {
             ExceptionHandler(task, exception);
         }
         catch (Exception exp)
         {
             // We can't do anything here... silently absorb it.
             InternalLogger.LogException(exception);
         }
     }
 }
示例#4
0
        private static void InternalTrace(string source, TraceEventType eventType, string message, string details)
        {
            // check if a connection string was provided
            if (_connectionString == null)
            {
                return;
            }

            SqlCommand cmd;

            try
            {
                cmd = GetTraceSqlCommand(source, eventType, message, details);
            }
            catch
            {
                return;
            }

            _threadPool.Enqueue(delegate
            {
                try
                {
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    InternalLogger.LogException(ex);
                }
                finally
                {
                    if (cmd.Connection.State == ConnectionState.Open)
                    {
                        // try and close connection
                        try
                        {
                            cmd.Connection.Close();
                        }
                        catch (Exception ex)
                        {
                            InternalLogger.LogException(ex);
                        }
                    }
                }
            });
        }
        public ApiResult <T> Post <T>(Uri uri, HttpContent content)
        {
            ApiResult <T> result;

            try
            {
                result = _decorated.Post <T>(uri, content);
            }
            catch (Exception ex)
            {
                InternalLogger.LogException(ex);

                result = new ApiResult <T>
                {
                    Exception = ApiException.Create(ex)
                };
            }

            return(result);
        }
        public async Task <ApiResult <T> > PostAsync <T>(Uri uri, HttpContent content)
        {
            ApiResult <T> result;

            try
            {
                result = await _decorated.PostAsync <T>(uri, content).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                InternalLogger.LogException(ex);

                result = new ApiResult <T>
                {
                    Exception = ApiException.Create(ex)
                };
            }

            return(result);
        }
示例#7
0
        internal void OnPreRequestHandlerExecute(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            if (httpContext.Response == null)
            {
                InternalLogger.LogException(new NullHttpResponseException(nameof(OnPreRequestHandlerExecute)));
                return;
            }

            if (httpContext.Response.Filter == null)
            {
                InternalLogger.LogException(new NullResponseFilterException(nameof(OnPreRequestHandlerExecute)));
                return;
            }

            httpContext.Response.Filter = new MirrorStreamDecorator(httpContext.Response.Filter, httpContext.Response.ContentEncoding);
        }
示例#8
0
        private static void DeleteFiles(IEnumerable <LoggedFile> files)
        {
            if (files == null || !files.Any())
            {
                return;
            }

            foreach (var item in files)
            {
                if (System.IO.File.Exists(item.FilePath))
                {
                    try
                    {
                        System.IO.File.Delete(item.FilePath);
                    }
                    catch (Exception ex)
                    {
                        InternalLogger.LogException(ex);
                    }
                }
            }
        }
示例#9
0
        internal void OnEndRequest(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            if (httpContext.Response == null)
            {
                InternalLogger.LogException(new NullHttpResponseException(nameof(OnEndRequest)));
                return;
            }

            if (httpContext.Request == null)
            {
                InternalLogger.LogException(new NullHttpRequestException(nameof(OnEndRequest)));
                return;
            }

            var    factory = new LoggerFactory();
            Logger logger  = factory.GetInstance(httpContext);

            // IIS redirect bypasses the IHttpModule.BeginRequest event
            if (logger.DataContainer.HttpProperties == null)
            {
                KissLog.Http.HttpRequest httpRequest = HttpRequestFactory.Create(httpContext.Request);
                logger.DataContainer.SetHttpProperties(new Http.HttpProperties(httpRequest));
            }

            MirrorStreamDecorator responseStream = GetResponseStream(httpContext.Response);
            long contentLength = responseStream == null ? 0 : responseStream.MirrorStream.Length;

            KissLog.Http.HttpResponse httpResponse = HttpResponseFactory.Create(httpContext.Response, contentLength);
            logger.DataContainer.HttpProperties.SetResponse(httpResponse);

            Exception ex = httpContext.Server?.GetLastError();

            if (ex != null)
            {
                logger.Error(ex);
            }

            if (responseStream != null)
            {
                if (KissLog.InternalHelpers.CanReadResponseBody(httpResponse.Properties.Headers))
                {
                    if (ShouldLogResponseBody(logger, factory, httpContext))
                    {
                        ILogResponseBodyStrategy logResponseBody = LogResponseBodyStrategyFactory.Create(responseStream.MirrorStream, responseStream.Encoding, logger);
                        logResponseBody.Execute();
                    }
                }

                responseStream.MirrorStream.Dispose();
            }

            IEnumerable <Logger> loggers = factory.GetAll(httpContext);

            KissLog.InternalHelpers.WrapInTryCatch(() =>
            {
                NotifyListeners.NotifyFlush.Notify(loggers.ToArray());
            });
        }
        public void LogExceptionDoesNotThrowExceptionWithuNullInternalLogDelegate()
        {
            KissLogConfiguration.InternalLog = null;

            InternalLogger.LogException(new Exception());
        }
 public void LogExceptionDoesNotThrowExceptionWithNullArgument()
 {
     InternalLogger.LogException(null);
 }
示例#12
0
        /// <summary>
        /// Main Loop that all worker threads run
        /// </summary>
        private void WorkerLoop()
        {
            ITask task = null;

            while (true)
            {
                try
                {
                    lock (taskQueue)
                    {
                        // If shutdown was requested, exit the thread.
                        if (IsShutdown())
                        {
                            return;
                        }

                        // Find a new work item to execute.
                        while (taskQueue.Count == 0)
                        {
                            threadsWaiting++;
                            try { Monitor.Wait(taskQueue); }
                            finally { threadsWaiting--; }

                            // If we were signaled due to shutdown, exit the thread.
                            if (IsShutdown())
                            {
                                return;
                            }
                        }

                        // We found a work item! Grab it
                        task = taskQueue.Dequeue();

                        // Record some statistics
                        if (task != null)
                        {
                            task.TimeDequeued = DateTime.UtcNow;  //Marking the time of dequeue
                            if (statsRecorder != null)
                            {
                                statsRecorder.RecordTaskStats(task, ThreadPoolActivity.DEQUEUED);
                            }
                        }
                    }

                    if (task != null)
                    {
                        Stopwatch stopWatch = Stopwatch.StartNew();

                        try
                        {
                            task.Execute();
                        }
                        finally
                        {
                            stopWatch.Stop();
                            task.ExecutionTime = stopWatch.Elapsed;
                            if (statsRecorder != null)
                            {
                                statsRecorder.RecordTaskStats(task, ThreadPoolActivity.EXECUTED);
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    InternalLogger.LogException(exception); // log to win32

                    // if this is a critical exception (e.x., out of memory) then we need to shut down this thread
                    if (IsCriticalException(exception))
                    {
                        throw;
                    }

                    // ThreadAbortException is automagically reraised, so don't try and stop it
                    // http://blogs.msdn.com/b/clrteam/archive/2009/04/28/threadabortexception.aspx
                    if (exception is ThreadAbortException)
                    {
                        return;
                    }

                    // Handle the exception that has occured
                    HandleException(task, exception);

                    // we will now restart the worker while loop
                }
            }
        }