Dispose() protected method

protected Dispose ( bool disposing ) : void
disposing bool
return void
Ejemplo n.º 1
0
        public void Test03()
        {
            string msg1, name;
            name = "MyXMLTraceWriter";
            string fileName = string.Format("{0}_3.xml", this.GetType().Name);

            //Testing ctor(Stream, string)
            //Scenario 3: Vanilla - Pass in a valid name and check with the Name property
            try
            {
                CommonUtilities.DeleteFile(fileName);
                _stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                _textWriter = new TextWriterTraceListener(_stream, name);
                Assert.True(_textWriter.Name == name);
                msg1 = "HelloWorld";
                _textWriter.WriteLine(msg1);
                _textWriter.Dispose();

                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(TextWriterTraceListener) }, new string[] { fileName }, msg1));
            }
            finally
            {
                CommonUtilities.DeleteFile(fileName);
            }
        }
Ejemplo n.º 2
0
        public void TraceTest01()
        {
            Refresh("TraceClassTests_TextWriterTraceListener1");
            try
            {
                using (FileStream fs = File.Create("TraceClassTests_TextWriterTraceListener1"))
                {
                    TextWriterTraceListener textTL = new TextWriterTraceListener(fs);
                    Trace.Listeners.Add(textTL);

                    Trace.WriteLine("Message start.");
                    Trace.IndentSize = 2;
                    Trace.IndentLevel = 2;
                    Trace.Write("This message should be indented.");
                    Trace.TraceError("This error not be indented.");
                    Trace.TraceError("{0}", "This error is indendented");
                    Trace.TraceWarning("This warning is indented");
                    Trace.TraceWarning("{0}", "This warning is also indented");
                    Trace.TraceInformation("This information in indented");
                    Trace.TraceInformation("{0}", "This information is also indented");
                    Trace.IndentSize = 0;
                    Trace.IndentLevel = 0;
                    Trace.WriteLine("Message end.");
                    textTL.Dispose();

                    Assert.True(HelperMethods.CheckStrings("TraceClassTests_TextWriterTraceListener1", String.Format("Message start.\r\n    This message should be indented.{0} Error: 0 : This error not be indented.\r\n    {0} Error: 0 : This error is indendented\r\n    {0} Warning: 0 : This warning is indented\r\n    {0} Warning: 0 : This warning is also indented\r\n    {0} Information: 0 : This information in indented\r\n    {0} Information: 0 : This information is also indented\r\nMessage end.\r\n", "DEFAULT_APPNAME"))); //DEFAULT_APPNAME this a bug which needs to be fixed.
                }
            }
            finally
            {
                Refresh("TraceClassTests_TextWriterTraceListener1");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Starts the trace listners.
        /// </summary>
        public static void StartTraceListners(string logFilePrefix = "RulePerf")
        {
            string logFilePath = "{1}-{0}.log".FormatWith(DateTime.Now.ToString("yyyy-MM-ddThh-mm-ssZ"), logFilePrefix);
            logFilePath = System.IO.Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location, logFilePath);
            TextWriterTraceListener textWriterTraceListener = new TextWriterTraceListener(logFilePath, "logListener");
            ConsoleTraceListener consoleTraceListener = new ConsoleTraceListener();

            ////Trace.Listeners.Clear();

            if (!Trace.Listeners.Contains(textWriterTraceListener.GetType(), textWriterTraceListener.Name))
            {
                Trace.Listeners.Add(textWriterTraceListener);
            }
            else
            {
                textWriterTraceListener.Dispose();
            }

            if (!Trace.Listeners.Contains(consoleTraceListener.GetType()))
            {
                Trace.Listeners.Add(consoleTraceListener);
            }
            else
            {
                consoleTraceListener.Dispose();
            }

            Trace.AutoFlush = true;
        }
Ejemplo n.º 4
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Enable debugging output to a logfile with the date and time of launch in the current directory
            Assembly _assembly = typeof(MainForm).Assembly;
            DateTime _now = DateTime.Now;
            logPath = Path.GetDirectoryName(_assembly.Location) + "\\" +
                _now.Year + "-" + _now.Month + "-" + _now.Day + "_" +
                _now.Hour + "-" + _now.Minute + "-" + _now.Second + ".log";

            Debug.WriteLine("[LogListener] Setting up Listener for log file " + logPath);
            TextWriterTraceListener logListener = new TextWriterTraceListener(logPath);
            Debug.Listeners.Add(logListener);
            Debug.WriteLine("[LogListener] Log file " + logPath + " created");

            // and start evetything
            Debug.WriteLine("[Program] Starting new MainForm");
            Application.Run(new MainForm());

            Debug.WriteLine("[Program] Returned from MainForm, cleaning up");
            logListener.Flush();
            logListener.Dispose();
        }
Ejemplo n.º 5
0
 public string HelloWorld()
 {
     TextWriterTraceListener list;
       Debug.Listeners.Add(list=new TextWriterTraceListener(Server.MapPath("../TraceLog1.txt")));
       Debug.Write("hello world " + DateTime.Now.ToString());
       Debug.Listeners.Remove(list);
       list.Close();
       list.Dispose();
       return "Hello World";
 }
Ejemplo n.º 6
0
 private void Page_Load(object sender, System.EventArgs e)
 {
     // this is page's trace will be appended to page if trace=true in page directive
       //this trace is diff from diagnostics trace
       Trace.Write("cat1","msg1");
       Trace.Warn("cat1","warn1");
       //will be displayed in debug and release mode - see output window
       TextWriterTraceListener list;
       System.Diagnostics.Trace.Listeners.Add(list = new TextWriterTraceListener(Server.MapPath("./TraceLog.txt")));//also write it to a file
       System.Diagnostics.Trace.Assert(false,"ha ha ha","he ha he heheh");//when fasle the text is written
       System.Diagnostics.Trace.WriteIf(true,"cat1","msg2");
       System.Diagnostics.Trace.Write("this","is not page tazce");
       System.Diagnostics.Trace.Listeners.Remove(list);
       list.Close();
       list.Dispose();
       //will not be displayed in debug mode
       Debug.WriteLine("cat1","debugmsg1");
       Debug.Assert(false,"ok","ok ok");
 }
Ejemplo n.º 7
0
        public void Test02()
        {
            try
            {
                //Testing ctor(Stream)
                //[] Vanilla - pass a valid stream. Also check via FileName return the expected value
                string msg1;
                _fileName = string.Format("{0}_2.xml", this.GetType().Name);
                CommonUtilities.DeleteFile(_fileName);

                _stream = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.Write);
                _textWriter = new TextWriterTraceListener(_stream);
                msg1 = "HelloWorld";
                _textWriter.WriteLine(msg1);
                _textWriter.Dispose();

                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(TextWriterTraceListener) }, new string[] { _fileName }, msg1));
            }
            finally
            {
                CommonUtilities.DeleteFile(_fileName);
            }
        }
Ejemplo n.º 8
0
 public Logger(string filePath)
 {
     Writer = new TextWriterTraceListener(filePath); Writer.Dispose();
 }
Ejemplo n.º 9
0
        public void Test04()
        {
            string msg1, name;
            name = null;
            _fileName = string.Format("{0}_4.xml", this.GetType().Name);

            //Scenario 4: name is null -its set as the empty string
            try
            {
                CommonUtilities.DeleteFile(_fileName);

                _stream = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.Write);
                _textWriter = new TextWriterTraceListener(_stream, name);
                Assert.True(_textWriter.Name == string.Empty);

                msg1 = "HelloWorld";
                _textWriter.WriteLine(msg1);
                _textWriter.Dispose();

                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(TextWriterTraceListener) }, new string[] { _fileName }, msg1));
            }
            finally
            {
                CommonUtilities.DeleteFile(_fileName);
            }
        }
Ejemplo n.º 10
0
        public void Test05()
        {
            string msg1;
            string[] names = { string.Empty, new string('a', 100000), "hell0<", "><&" };
            string fileName = string.Format("{0}_5.xml", this.GetType().Name);
            //Scenario 6: Other interesting string values - empty, very long, interesting characters (ones that will clash with xml like <, >, etc)
            try
            {
                foreach (string name in names)
                {
                    CommonUtilities.DeleteFile(fileName);
                    _stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                    _textWriter = new TextWriterTraceListener(_stream, name);

                    Assert.True(_textWriter.Name == name);
                    msg1 = "HelloWorld";
                    _textWriter.WriteLine(msg1);
                    _textWriter.Dispose();

                    Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(TextWriterTraceListener) }, new string[] { fileName }, msg1));
                }
            }
            finally
            {
                CommonUtilities.DeleteFile(fileName);
            }
        }
Ejemplo n.º 11
0
        public void TraceTest02()
        {
            //Check the same using Indent() and Unindent() message.
            Refresh("TraceClassTests_TextWriterTraceListener2");
            try
            {
                using (FileStream fs = File.Create("TraceClassTests_TextWriterTraceListener2"))
                {
                    TextWriterTraceListener textTL = new TextWriterTraceListener(fs);
                    Trace.Listeners.Add(textTL);
                    Trace.IndentSize = 2;
                    Trace.WriteLineIf(true, "Message start.");
                    Trace.Indent();
                    Trace.Indent();
                    Trace.WriteIf(true, "This message should be indented.");
                    Trace.WriteIf(false, "This message should be ignored.");
                    Trace.Indent();
                    Trace.WriteLine("This should not be indented.");
                    Trace.WriteLineIf(false, "This message will be ignored");
                    Trace.Fail("This failure is reported", "with a detailed message");
                    Trace.Assert(false);
                    Trace.Assert(false, "This assert is reported");
                    Trace.Assert(true, "This assert is not reported");
                    Trace.Unindent();
                    Trace.Unindent();
                    Trace.Unindent();
                    Trace.WriteLine("Message end.");
                    textTL.Dispose();

                    Assert.True(HelperMethods.CheckStrings("TraceClassTests_TextWriterTraceListener2", "Message start.\r\n    This message should be indented.This should not be indented.\r\n      Fail: This failure is reported with a detailed message\r\n      Fail: \r\n      Fail: This assert is reported\r\nMessage end.\r\n"));
                }
            }
            finally
            {
                Refresh("TraceClassTests_TextWriterTraceListener2");
            }
        }
Ejemplo n.º 12
0
 void Logging()
 {
     try
     {
         if (Log == false)
         {
             Log = true;
             Thread thread = new Thread(Boosting);
             thread.IsBackground = true;
             thread.Start();
             Directory.CreateDirectory(LogPath);
             if (!File.Exists(LogPath + @"\GClientService.Log"))
             {
                 var createLog = File.Create(LogPath + @"\GClientService.Log");
                 createLog.Dispose();
             }
             if (File.Exists(LogPath + @"\GClientService.Log"))
             {
                 File.Delete(LogPath + @"\GClientService-Previous.log");
             }
             bool TempExist = true;
             while (TempExist)
             {
                 if (File.Exists(LogPath + @"\GClientService-Previous.log"))
                 {
                     Thread.Sleep(50);
                 }
                 else
                 {
                     File.Move(LogPath + @"\GClientService.Log", LogPath + @"\GClientService-Previous.log");
                     TempExist = false;
                 }
             }
             Trace.Listeners.Clear();
             TextWriterTraceListener twtl = null;
             try
             {
                 twtl = new TextWriterTraceListener(LogPath + @"\GClientService.Log");
                 ConsoleTraceListener ctl = null;
                 try
                 {
                     ctl = new ConsoleTraceListener(false);
                     Trace.Listeners.Add(twtl);
                     Trace.Listeners.Add(ctl);
                     Trace.AutoFlush = true;
                 }
                 finally
                 {
                     if (ctl != null)
                     {
                         ctl.Dispose();
                     }
                 }
             }
             finally
             {
                 if (twtl != null)
                 {
                     twtl.Dispose();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Trace.WriteLine(DateTime.Now + Properties.Resources.ResourceManager.GetString("FailedOperationMessage") + "\n" + "          " + ex.Message);
     }
 }
        public void TracingEventListenerDoesNotDeadlock()
        {
            QueuingEventListener upstreamListener = new QueuingEventListener();
            EventQueue upstreamListenerQueue = upstreamListener.Events;

            // Install a TraceListener sending TestOutput events to the upstreamListener.
            // This simulates RemoteTestRunner.StartTextCapture, where TestContext installs such a TraceListener.
            TextWriter traceWriter = new EventListenerTextWriter(upstreamListener, TestOutputType.Trace);
            const string TraceListenerName = "TracingEventListenerDoesNotDeadlock";
            TraceListener feedingTraceToUpstreamListener = new TextWriterTraceListener(traceWriter, TraceListenerName);

            try
            {
                Trace.Listeners.Add(feedingTraceToUpstreamListener);

                // downstreamListenerToTrace simulates an EventListener installed e.g. by an Addin, 
                // which may call Trace within the EventListener methods:
                TracingEventListener downstreamListenerToTrace = new TracingEventListener();
                using (EventPump pump = new EventPump(downstreamListenerToTrace, upstreamListenerQueue, false))
                {
                    pump.Name = "TracingEventListenerDoesNotDeadlock";
                    pump.Start();

                    const int Repetitions = 10;
                    for (int i = 0; i < Repetitions; i++)
                    {
                        foreach (Event e in events)
                        {
                            Trace.WriteLine("Before sending {0} event.", e.GetType().Name);
                            e.Send(upstreamListener);
                            Trace.WriteLine("After sending {0} event.", e.GetType().Name);
                        }
                    }
                }
            }
            finally
            {
                Trace.Listeners.Remove(TraceListenerName);
                feedingTraceToUpstreamListener.Dispose();
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Function used to start the file stream to which the trace is written.
        /// </summary>
        private static void InitializeTraceFileStream()
        {
            //Trace file stream exists, return.
            if (m_IsTraceFileStreamInitialized)
            {
                return;
            }

            try
            {
                FileStream traceFileStream = null;
                TraceLogFilePolicy traceLogFilePolicy = new TraceLogFilePolicy
                    (LoggingConstants.CONS_TRACE_FILE_BASE_NAME, m_TraceInfo.FileSizeInMB, LoggingConstants.CONS_TOTAL_TRACE_FILES);
                TextWriterTraceListener textWriterTraceListener = null;
                try
                {
                    traceFileStream = traceLogFilePolicy.GetLogFileStream();
                    textWriterTraceListener = new TextWriterTraceListener(traceFileStream);
                    Trace.Listeners.RemoveAt(0); //Remove the default trace listener
                    Trace.Listeners.Add(textWriterTraceListener);
                    m_IsTraceFileStreamInitialized = true;
                }
                catch (Exception ex)
                {
                    m_TraceInfo.IsTracingEnabled = false;
                    if (textWriterTraceListener != null) textWriterTraceListener.Dispose();
                    if (traceFileStream != null) traceFileStream.Dispose();
                    throw ex;
                }
            }
            catch (OMEException ObjOMEException)
            {
                LoggingHelper.ShowOMEMessage(ObjOMEException);
            }
            catch (Exception ex)
            {
                LoggingHelper.ShowMessage(ex);
            }
        }
Ejemplo n.º 15
0
 protected override void OnDragOver(DragEventArgs e)
 {
     endX = e.X;
       endY = e.Y;
       TextWriterTraceListener list;
       Debug.Listeners.Add(list=new TextWriterTraceListener(System.Web.HttpContext.Current.Server.MapPath("../TraceLog1.txt")));
       Debug.Write("x: " +startX + " y: "+ startY + "; x1: " + endX  + " y1: " +endY + DateTime.Now.ToString());
       Debug.Listeners.Remove(list);
       list.Close();
       list.Dispose();
       this.Refresh();
 }
Ejemplo n.º 16
0
        private bool disposedValue = false; // To detect redundant calls

        #endregion Fields

        #region Constructors

        //Main Environment Startup
        public MainWindow()
        {
            try
            {
                Hide();
                Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentCulture;
                InitializeComponent();
                //Logging
                try
                {
                    Variables.worker = new BackgroundWorker();
                    Directory.CreateDirectory(Variables.LogPath);
                    Directory.CreateDirectory(Variables.ImagePath);
                    if (!File.Exists(Variables.LogPath + @"\GClient.Log"))
                    {
                        var createLog = File.Create(Variables.LogPath + @"\GClient.Log");
                        createLog.Dispose();
                    }
                    if (File.Exists(Variables.LogPath + @"\GClient.Log"))
                    {
                        File.Delete(Variables.LogPath + @"\GClient-Previous.log");
                    }
                    bool TempExist = true;
                    while (TempExist)
                    {
                        if (File.Exists(Variables.LogPath + @"\GClient-Previous.log"))
                        {
                            Thread.Sleep(50);
                        }
                        else
                        {
                            File.Move(Variables.LogPath + @"\GClient.Log", Variables.LogPath + @"\GClient-Previous.log");
                            TempExist = false;
                        }
                    }
                    Trace.Listeners.Clear();
                    TextWriterTraceListener twtl = null;
                    try
                    {
                        twtl = new TextWriterTraceListener(Variables.LogPath + @"\GClient.Log");
                        ConsoleTraceListener ctl = null;
                        try
                        {
                            ctl = new ConsoleTraceListener(false);
                            Trace.Listeners.Add(twtl);
                            Trace.Listeners.Add(ctl);
                            Trace.AutoFlush = true;
                            Trace.WriteLine(DateTime.Now + " GClient GT is initializing: ");
                        }
                        finally
                        {
                            if (ctl != null)
                            {
                                ctl.Dispose();
                            }
                        }
                    }
                    finally
                    {
                        if (twtl != null)
                        {
                            twtl.Dispose();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(DateTime.Now + Properties.Resources.ResourceManager.GetString("FailedOperationMessage") + "\n" + "          " + ex.Message);
                    MessageBox.Show(Properties.Resources.ResourceManager.GetString("FailedOperationMessage") + "\n\n" + ex.Message, Properties.Resources.ResourceManager.GetString("FailedOperationTitle"), MessageBoxButton.OK, MessageBoxImage.Stop);
                }
                Variables.GUIShowing = false;
                label2.Content = "Version: " + System.Windows.Forms.Application.ProductVersion;
                SettingsFile();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(DateTime.Now + Properties.Resources.ResourceManager.GetString("FailedOperationMessage") + "\n" + "          " + ex.Message);
                MessageBox.Show(Properties.Resources.ResourceManager.GetString("FailedOperationMessage") + "\n\n" + ex.Message, Properties.Resources.ResourceManager.GetString("FailedOperationTitle"), MessageBoxButton.OK, MessageBoxImage.Stop);
            }
        }