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)); }
public TextLogSpy(string loggerName, string pattern) { stringBuilder = new StringBuilder(); appender = new TextWriterAppender { Layout = new PatternLayout(pattern), Threshold = Level.All, Writer = new StringWriter(stringBuilder) }; loggerImpl = (Logger)LogManager.GetLogger(loggerName).Logger; loggerImpl.AddAppender(appender); loggerImpl.Level = Level.All; }
private long GetLog4NetTimestamp(DateTime dateTime) { var logXmlBuilder = new StringBuilder (); using (var writer = new StringWriter (logXmlBuilder)) { var layout = new XmlLayoutSchemaLog4j (); var appender = new TextWriterAppender { ImmediateFlush = true, Layout = layout, Writer = writer }; var repository = LogManager.GetRepository (Assembly.GetCallingAssembly ()); var eventData = new LoggingEventData { LoggerName = "TestLogger", TimeStamp = dateTime, Level = log4net.Core.Level.Debug, ThreadName = "TestThread", Message = "Test message." }; var @event = new LoggingEvent (typeof (TimestampTests), repository, eventData); appender.DoAppend (@event); writer.Flush (); appender.Close (); } var logXml = logXmlBuilder.ToString (); const string timestampStartMarker = "timestamp=\""; var timestampStart = logXml.IndexOf (timestampStartMarker) + timestampStartMarker.Length; Assume.That (timestampStart, Is.GreaterThanOrEqualTo (0)); var timestampEnd = logXml.IndexOf ('"', timestampStart); Assume.That (timestampEnd, Is.GreaterThanOrEqualTo (0)); var timestampString = logXml.Substring (timestampStart, timestampEnd - timestampStart); var timestamp = Int64.Parse (timestampString); return timestamp; }
static void Main(string[] args) { string logpath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(SheduleVmpsConfig)).Location)+"\\Shedulevmps.Log.Log"; TextWriter tw = File.CreateText(logpath); var textWriterAppender = new TextWriterAppender() { Layout = new log4net.Layout.PatternLayout("%d %-5p %method :> %m%n"), Writer = tw, ImmediateFlush = true }; var consoleAppender = new ConsoleAppender { Layout = textWriterAppender.Layout }; BasicConfigurator.Configure(new IAppender[] { textWriterAppender, consoleAppender }); GetVirtualMachineStatus(args); }
private void MainForm_Load( object sender, EventArgs e ) { TextWriterAppender textWriterAppender = new TextWriterAppender( ); textWriterAppender.Layout = new SimpleLayout( ); textWriterAppender.Writer = new StreamWriter( new LogStream( this.resultTextBox ) ); BasicConfigurator.Configure( textWriterAppender ); tabControl2.SelectedIndex = 1; }
/// <summary> /// Initialises the stream logger. /// </summary> public void init() { #if LOG_ON PatternLayout layout = new PatternLayout("%r [%t] %p %c %x - %m%n"); StreamHandler writer = new StreamHandler(this); TextWriterAppender appender= new TextWriterAppender(); appender.Layout = layout; appender.Writer = writer; Hierarchy h = LogManager.GetRepository()as Hierarchy; h.Root.AddAppender(appender); #endif }