public void FlatFileListenerWillFallbackIfNotPriviledgesToWrite()
        {
            string fileName = @"trace.log";
            string fullPath = String.Format(@"{0}\{1}", Directory.GetCurrentDirectory(), fileName);

            File.Delete(fileName);

            FileIOPermission fileIOPerm1 = new FileIOPermission(PermissionState.None);

            fileIOPerm1.SetPathList(FileIOPermissionAccess.Read, fullPath);
            fileIOPerm1.PermitOnly();

            try
            {
                FlatFileTraceListener listener = new FlatFileTraceListener(fileName, "---header---", "***footer***", new TextFormatter("DUMMY{newline}DUMMY"));

                // need to go through the source to get a TraceEventCache
                LogSource source = new LogSource("notfromconfig", SourceLevels.All);
                source.Listeners.Add(listener);
                source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));
                source.Dispose();
            }
            catch (SecurityException)
            {
                FileIOPermission.RevertAll();
                throw;
            }
        }
        public void FlatFileListenerWillFallbackIfNotPriviledgesToWrite()
        {
            string fileName = @"trace.log";
            string fullPath = String.Format(@"{0}\{1}", Directory.GetCurrentDirectory(), fileName);

            File.Delete(fileName);

            FileIOPermission fileIOPerm1 = new FileIOPermission(PermissionState.None);
            fileIOPerm1.SetPathList(FileIOPermissionAccess.Read, fullPath);
            fileIOPerm1.PermitOnly();

            try
            {
                FlatFileTraceListener listener = new FlatFileTraceListener(fileName,"---header---","***footer***",new TextFormatter("DUMMY{newline}DUMMY"));

                // need to go through the source to get a TraceEventCache
                LogSource source = new LogSource("notfromconfig", SourceLevels.All);
                source.Listeners.Add(listener);
                source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));
                source.Dispose();
            }
            catch (SecurityException)
            {
                FileIOPermission.RevertAll();
                throw;
            }
        }
        public void ListenerWithHeaderAndFooterWillUseFormatterIfExists()
        {
			File.Delete("tracewithheaderandfooter.log");  
        	
            FlatFileTraceListener listener = new FlatFileTraceListener("tracewithheaderandfooter.log", "--------header------", "=======footer===========", new TextFormatter("DUMMY{newline}DUMMY"));

            // need to go through the source to get a TraceEventCache
            LogSource source = new LogSource("notfromconfig", SourceLevels.All);
            source.Listeners.Add(listener);
            source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));
            source.Dispose();

            string strFileContents = GetFileContents("tracewithheaderandfooter.log");

            Assert.AreEqual("--------header------" + Environment.NewLine + "DUMMY" + Environment.NewLine + "DUMMY" + Environment.NewLine + "=======footer===========" + Environment.NewLine, strFileContents);
        }
        public void ListenerWithHeaderAndFooterWillUseFormatterIfExists()
        {
            File.Delete("tracewithheaderandfooter.log");

            FlatFileTraceListener listener = new FlatFileTraceListener("tracewithheaderandfooter.log", "--------header------", "=======footer===========", new TextFormatter("DUMMY{newline}DUMMY"));

            // need to go through the source to get a TraceEventCache
            LogSource source = new LogSource("notfromconfig", SourceLevels.All);

            source.Listeners.Add(listener);
            source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));
            source.Dispose();

            string strFileContents = GetFileContents("tracewithheaderandfooter.log");

            Assert.AreEqual("--------header------" + Environment.NewLine + "DUMMY" + Environment.NewLine + "DUMMY" + Environment.NewLine + "=======footer===========" + Environment.NewLine, strFileContents);
        }
		public void ListenerWillFallbackToTraceEntryToStringIfFormatterDoesNotExists()
		{
			LogEntry testLogEntry = new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null);
            StreamWriter writer = new StreamWriter("trace.log");
            FlatFileTraceListener listener = new FlatFileTraceListener(writer);

			// need to go through the source to get a TraceEventCache
			LogSource source = new LogSource("notfromconfig", SourceLevels.All);
			source.Listeners.Add(listener);
			source.TraceData(TraceEventType.Error, 0, testLogEntry);
            source.Dispose();

            string strFileContents = GetFileContents("trace.log");

			string testLogEntryAsString = testLogEntry.ToString();

			Assert.IsTrue(-1 != strFileContents.IndexOf(testLogEntryAsString));
		}
        public void ListenerWillFallbackToTraceEntryToStringIfFormatterDoesNotExists()
        {
            LogEntry              testLogEntry = new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null);
            StreamWriter          writer       = new StreamWriter("trace.log");
            FlatFileTraceListener listener     = new FlatFileTraceListener(writer);

            // need to go through the source to get a TraceEventCache
            LogSource source = new LogSource("notfromconfig", SourceLevels.All);

            source.Listeners.Add(listener);
            source.TraceData(TraceEventType.Error, 0, testLogEntry);
            source.Dispose();

            string strFileContents = GetFileContents("trace.log");

            string testLogEntryAsString = testLogEntry.ToString();

            Assert.IsTrue(-1 != strFileContents.IndexOf(testLogEntryAsString));
        }
        public void FlatFileTraceListenerMultipleWrites()
        {
            File.Delete("tracewithheaderandfooter.log");
            string header         = "--------header------";
            int    numberOfWrites = 4;

            FlatFileTraceListener listener = new FlatFileTraceListener("tracewithheaderandfooter.log", header, "=======footer===========", new TextFormatter("DUMMY{newline}DUMMY"));

            // need to go through the source to get a TraceEventCache
            LogSource source = new LogSource("notfromconfig", SourceLevels.All);

            source.Listeners.Add(listener);
            for (int writeLoop = 0; writeLoop < numberOfWrites; writeLoop++)
            {
                source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));
            }
            source.Dispose();

            StreamReader reader       = new StreamReader("tracewithheaderandfooter.log");
            int          headersFound = NumberOfItems("tracewithheaderandfooter.log", header);

            Assert.AreEqual(numberOfWrites, headersFound);
        }
	    public void FlatFileTraceListenerMultipleWrites()
	    {
	        File.Delete("tracewithheaderandfooter.log");
	        string header = "--------header------";
	        int numberOfWrites = 4;

	        FlatFileTraceListener listener = new FlatFileTraceListener("tracewithheaderandfooter.log", header, "=======footer===========", new TextFormatter("DUMMY{newline}DUMMY"));
            
	        // need to go through the source to get a TraceEventCache
	        LogSource source = new LogSource("notfromconfig", SourceLevels.All);
	        source.Listeners.Add(listener);
	        for (int writeLoop = 0; writeLoop < numberOfWrites; writeLoop++)
	            source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));
            source.Dispose();

	        StreamReader reader = new StreamReader("tracewithheaderandfooter.log");
            int headersFound = NumberOfItems("tracewithheaderandfooter.log", header);

	        Assert.AreEqual(numberOfWrites,headersFound);
	    }