Example #1
0
        public void Logger_Log_DelegatesToNLog()
        {
            var     target = GetLogMemoryTarget();
            ILogger log    = new NLogLogger(LogManager.GetLogger("cat"));

            log.Info("msg");
            Assert.Equal("cat|Info|msg||", target.Logs.Last());
            log.Log(LogLevel.Info, "msg");
            Assert.Equal("cat|Info|msg||", target.Logs.Last());

            log.Debug("msg");
            Assert.Equal("cat|Debug|msg||", target.Logs.Last());
            log.Log(LogLevel.Debug, "msg");
            Assert.Equal("cat|Debug|msg||", target.Logs.Last());

            log.Warn("msg");
            Assert.Equal("cat|Warn|msg||", target.Logs.Last());
            log.Log(LogLevel.Warning, "msg");
            Assert.Equal("cat|Warn|msg||", target.Logs.Last());

            log.Error("msg", new Exception("ex"));
            Assert.Equal("cat|Error|msg|ex|", target.Logs.Last());
            log.Log(LogLevel.Error, "msg", new Exception("ex"));
            Assert.Equal("cat|Error|msg|ex|", target.Logs.Last());

            log.Fatal("msg");
            Assert.Equal("cat|Fatal|msg||", target.Logs.Last());
            log.Fatal("msg", new Exception("ex"));
            Assert.Equal("cat|Fatal|msg|ex|", target.Logs.Last());
            log.Log(LogLevel.Fatal, "msg", new Exception("ex"));
            Assert.Equal("cat|Fatal|msg|ex|", target.Logs.Last());
        }
Example #2
0
        public void CallingMethods()
        {
            NLogLogMock logMock = new NLogLogMock();
            NLogLogger  logger  = new NLogLogger(logMock);
            bool        b       = logger.IsDebugEnabled;

            b = logger.IsErrorEnabled;
            b = logger.IsFatalEnabled;
            b = logger.IsInfoEnabled;
            b = logger.IsWarnEnabled;

            logger.Debug(null);
            logger.Debug(null, null);
            logger.DebugFormat(null, null);

            logger.Error(null);
            logger.Error(null, null);
            logger.ErrorFormat(null, null);

            logger.Warn(null);
            logger.Warn(null, null);
            logger.WarnFormat(null, null);

            logger.Info(null);
            logger.Info(null, null);
            logger.InfoFormat(null, null);

            logger.Fatal(null);
            logger.Fatal(null, null);

            logMock.debug.Should().Be(1);
            logMock.debugException.Should().Be(1);
            logMock.debugFormat.Should().Be(1);
            logMock.info.Should().Be(1);
            logMock.infoException.Should().Be(1);
            logMock.infoFormat.Should().Be(1);
            logMock.warn.Should().Be(1);
            logMock.warnException.Should().Be(1);
            logMock.warnFormat.Should().Be(1);
            logMock.error.Should().Be(1);
            logMock.errorException.Should().Be(1);
            logMock.errorFormat.Should().Be(1);
            logMock.fatal.Should().Be(1);
            logMock.fatalException.Should().Be(1);
            logMock.isDebugEnabled.Should().Be(1);
            logMock.isInfoEnabled.Should().Be(1);
            logMock.isWarnEnabled.Should().Be(1);
            logMock.isErrorEnabled.Should().Be(1);
            logMock.isFatalEnabled.Should().Be(1);
        }
        public void Add_File_Target()
        {
            string tmpFile = Path.GetTempFileName();
            ILog   log     = new NLogLogger("Test_Logger");

            log.AddFileTarget("Test_Target", tmpFile);
            log.Debug("DEBUG MESSAGE");
            log.Info("INFO MESSAGE");
            log.Error("ERROR MESSAGE");
            log.Warn("WARN MESSAGE");
            log.Fatal("FATAL MESSAGE");
            log.Flush();

            string logContents = File.ReadAllText(tmpFile);

            Console.Write(logContents);

            Assert.True(logContents.Contains("DEBUG MESSAGE"));
            Assert.True(logContents.Contains("INFO MESSAGE"));
            Assert.True(logContents.Contains("ERROR MESSAGE"));
            Assert.True(logContents.Contains("WARN MESSAGE"));
            Assert.True(logContents.Contains("FATAL MESSAGE"));

            File.Delete(tmpFile);
        }
Example #4
0
        public void WhenCallingFatalShouldRecordOneError()
        {
            // act
            _logger.Fatal("Test String", new NotImplementedException());

            // assert
            _memoryTarget.Logs.Count.Should().Be(1, "because we only called the method once");
            _memoryTarget.Logs.All(log => log.Contains("Fatal")).Should().BeTrue("Because we only logged a Fatal");
        }
Example #5
0
        private static void LogTest()
        {
            var logger = new NLogLogger(new ApplicationSettings());

            logger.Debug("Just testing debug", "Debug");
            logger.Info("Just testing info", "Info");
            logger.Warn("Just testing warning", "Warning");
            logger.Error("Just testing error", "Error");
            logger.Fatal("Testing with exception", new Exception("TestException"), "Some details again");
        }
Example #6
0
        protected void Application_Error(object sender, EventArgs e)
        {
            var ex = Server.GetLastError();

            _logger.Error("application error::" + Utils.FormatError(ex), ex, CommonEnums.LoggerObjectTypes.Application);
            try
            {
                _logger.Fatal("application error::" + HttpContext.Current.Request.RawUrl + "::" + HttpContext.Current.Request.QueryString, ex);
            }
            catch (Exception x)
            {
                _logger.Error("application error::", x, CommonEnums.LoggerObjectTypes.Application);
            }

            var exception = ex as HttpException;

            if (exception != null && exception.GetHttpCode() == 404)
            {
                try
                {
                    Response.StatusCode = 404;
                    var ctx = HttpContext.Current;

                    //var error = new KeyValuePair<string, object>("ErrorMessage", ctx.Server.GetLastError().ToString());

                    ctx.Response.Clear();

                    var rc = ((MvcHandler)ctx.CurrentHandler).RequestContext;

                    var factory = ControllerBuilder.Current.GetControllerFactory();

                    var controller = (HomeController)factory.CreateController(rc, "home");
                    var cc         = new ControllerContext(rc, controller);

                    controller.ControllerContext = cc;

                    var s = controller.RenderRazorViewToString("Error404", null);

                    Response.Write(s);
                    ctx.Response.End();
                }
                catch (Exception) { }
            }
            else
            {
                var ee = Utils.FormatError(ex).OptimizedUrl();
                Response.Redirect("/Home/Error/");
            }
        }
 public static void Start()
 {
     try
     {
         IContainer container = IoC.Initialize();
         StructureMapDependencyScope = new StructureMapDependencyScope(container);
         DependencyResolver.SetResolver(StructureMapDependencyScope);
         DynamicModuleUtility.RegisterModule(typeof(StructureMapScopeModule));
     }
     catch (Exception ex)
     {
         ILog logger = new NLogLogger();;
         logger.Fatal(ex, "Error Configuring StructureMap");
     }
 }
        internal static void Main(string[] args)
        {
            var logger = new NLogLogger(null, null, null);

            try
            {
                logger.Info("WebJob starting");
                var container = IoC.Initialize();

                var updater = container.GetInstance <IEducationalOrgsUpdater>();
                updater.RunUpdate().Wait();
                logger.Info("WebJob finished");
            }
            catch (Exception ex)
            {
                logger.Fatal(ex, "Unhandled Exception");
            }
        }
 public void Fatal(Exception ex, string message)
 {
     _nLogLogger.Fatal(ex, message);
     _traceWriterLogger.Fatal(ex, message);
 }