Fix TraceSource constructor. Fixes #12001
authorMarek Safar <marek.safar@gmail.com>
Thu, 9 May 2013 11:54:13 +0000 (13:54 +0200)
committerMarek Safar <marek.safar@gmail.com>
Thu, 9 May 2013 12:52:36 +0000 (14:52 +0200)
mcs/class/System/System.Diagnostics/TraceSource.cs
mcs/class/System/Test/System.Diagnostics/TraceSourceTest.cs

index a99e77a2e6fd3825e05e454478ac91684bee921b..e804235386a69b9cc065b77925c37c4ba94a8890 100644 (file)
@@ -1,12 +1,12 @@
 //
 // TraceSource.cs
 //
-// Author:
+// Authors:
 //     Atsushi Enomoto  <atsushi@ximian.com>
+//     Marek Safar (marek.safar@gmail.com)
 //
 // Copyright (C) 2007 Novell, Inc.
-//
-
+// Copyright 2013 Xamarin Inc
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -45,17 +45,21 @@ namespace System.Diagnostics
                {
                }
 
-               public TraceSource (string name, SourceLevels sourceLevels)
+               public TraceSource (string name, SourceLevels defaultLevel)
                {
                        if (name == null)
                                throw new ArgumentNullException ("name");
+                       if (name.Length == 0)
+                               throw new ArgumentException ("name");
+                       
                        Hashtable sources = DiagnosticsConfiguration.Settings ["sources"] as Hashtable;
                        TraceSourceInfo info = sources != null ? sources [name] as TraceSourceInfo : null;
                        source_switch = new SourceSwitch (name);
 
-                       if (info == null)
+                       if (info == null) {
                                listeners = new TraceListenerCollection ();
-                       else {
+                               source_switch.Level = defaultLevel;
+                       } else {
                                source_switch.Level = info.Levels;
                                listeners = info.Listeners;
                        }
index a95f54b90afbd059153ef456e9a50979a5f55306..562c6e9dc6f531c27db6bdb7093b69387be02502 100644 (file)
@@ -50,6 +50,13 @@ namespace MonoTests.System.Diagnostics
                        new TraceSource (null);
                }
 
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void ConstructorEmpty ()
+               {
+                       new TraceSource ("");
+               }
+               
                [Test]
                public void DefaultValues ()
                {
@@ -70,6 +77,20 @@ namespace MonoTests.System.Diagnostics
                        TraceSource ts = new TraceSource ("foo");
                        ts.Switch = null;
                }
+               
+               [Test]
+               public void SwitchLevel ()
+               {
+                       TraceSource s = new TraceSource ("Source1");
+                       Assert.AreEqual (SourceLevels.Off, s.Switch.Level, "#1");
+
+                       s = new TraceSource("Source2", SourceLevels.All);
+                       Assert.AreEqual (SourceLevels.All, s.Switch.Level, "#2");
+
+                       s = new TraceSource("Source3");
+                       s.Switch.Level = SourceLevels.All;
+                       Assert.AreEqual (SourceLevels.All, s.Switch.Level, "#3");
+               }
        }
 }