public void Test_LogHttpRequest()
        {
            // Force log initialization
            Logger logger = new Logger(typeof(HttpUtilitiesTest).ToString());

            // Reset current configuration
            LogManager.ResetConfiguration();

            // Redirect log output to strinb guilder
            StringBuilder      sb       = new StringBuilder();
            TextWriterAppender appender = new TextWriterAppender();

            appender.Writer    = new StringWriter(sb);
            appender.Threshold = log4net.Core.Level.Debug;
            appender.Layout    = new log4net.Layout.PatternLayout();
            appender.ActivateOptions();
            log4net.Config.BasicConfigurator.Configure(appender);

            // Initialize current request
            string userIdentity = Guid.NewGuid().ToString();

            MockHttpContext.InitializeCurrentContext();
            HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(userIdentity), null);

            // Log and test
            Esapi.HttpUtilities.LogHttpRequest(HttpContext.Current.Request, Esapi.Logger, null);
            Assert.IsFalse(string.IsNullOrEmpty(sb.ToString()));
            Assert.IsTrue(sb.ToString().Contains(userIdentity));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Tracing setup.
        /// </summary>
        public void InitTraceFile(SessionInfo si)
        {
            if (logger_output != null)
            {
                throw new ApplicationException("Already called."); // is seems this object is designed so that it stores at max one session per lifetime
            }
            var id = si.ID;

            // create tracer
            // =============

            int Rank = MyRank;

            Stream tracerfile = null;

            if (fsDriver != null && id != Guid.Empty)
            {
                tracerfile = this.FsDriver.GetNewLogStream("trace." + Rank, id);
            }

            TextWriter tracertxt = null;

            if (tracerfile == null && Tracer.NamespacesToLog.Length > 0 && !configAllreadyDone)
            {
                // create trace file in local directory

                string tracefilename = "trace." + Rank + ".txt";
                tracerfile = new FileStream(tracefilename, FileMode.Create, FileAccess.Write, FileShare.Read);
            }

            if (tracerfile != null)
            {
                //var zipper = new System.IO.Compression.GZipStream(tracerfile, System.IO.Compression.CompressionMode.Compress);
                //tracertxt = new StreamWriter(zipper);
                tracertxt = new StreamWriter(tracerfile);
            }

            if (tracertxt != null)
            {
                TextWriterAppender fa = new TextWriterAppender();
                fa.ImmediateFlush = true;
                //fa.Writer = Console.Out;
                fa.Writer = tracertxt;
                fa.Layout = new PatternLayout("%date %-5level %logger: %message%newline");
                fa.ActivateOptions();
                BasicConfigurator.Configure(fa);
                logger_output      = fa;
                configAllreadyDone = true;
            }
        }
Ejemplo n.º 3
0
        // ------------------------------------------------------------------------------------

        private void _BeginStoreLogMessages()
        {
            logMessages         = new StringBuilder(8192);
            appenderLogMessages = new TextWriterAppender();
            appenderLogMessages.ImmediateFlush = true;
            appenderLogMessages.Layout         = new log4net.Layout.PatternLayout("%date %-5level %message%newline");
            appenderLogMessages.Writer         = new StringWriter(logMessages);

            appenderLogMessages.Threshold = Level.Info;

            IAppenderAttachable appender_attachable = log.Logger as IAppenderAttachable;

            if (appender_attachable != null)
            {
                appender_attachable.AddAppender(appenderLogMessages);
            }

            appenderLogMessages.ActivateOptions();
        }