diff --git a/src/libraries/System.Diagnostics.TextWriterTraceListener/ref/System.Diagnostics.TextWriterTraceListener.cs b/src/libraries/System.Diagnostics.TextWriterTraceListener/ref/System.Diagnostics.TextWriterTraceListener.cs index 3e516deef81a4..6e95ed14edbf5 100644 --- a/src/libraries/System.Diagnostics.TextWriterTraceListener/ref/System.Diagnostics.TextWriterTraceListener.cs +++ b/src/libraries/System.Diagnostics.TextWriterTraceListener/ref/System.Diagnostics.TextWriterTraceListener.cs @@ -16,50 +16,50 @@ public override void Close() { } public partial class DelimitedListTraceListener : System.Diagnostics.TextWriterTraceListener { public DelimitedListTraceListener(System.IO.Stream stream) { } - public DelimitedListTraceListener(System.IO.Stream stream, string name) { } + public DelimitedListTraceListener(System.IO.Stream stream, string? name) { } public DelimitedListTraceListener(System.IO.TextWriter writer) { } - public DelimitedListTraceListener(System.IO.TextWriter writer, string name) { } - public DelimitedListTraceListener(string fileName) { } - public DelimitedListTraceListener(string fileName, string name) { } + public DelimitedListTraceListener(System.IO.TextWriter writer, string? name) { } + public DelimitedListTraceListener(string? fileName) { } + public DelimitedListTraceListener(string? fileName, string? name) { } public string Delimiter { get { throw null; } set { } } protected override string[] GetSupportedAttributes() { throw null; } - public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data) { } - public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, params object[] data) { } - public override void TraceEvent(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string message) { } - public override void TraceEvent(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string format, params object[] args) { } + public override void TraceData(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object? data) { } + public override void TraceData(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, params object?[]? data) { } + public override void TraceEvent(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string? message) { } + public override void TraceEvent(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string format, params object?[]? args) { } } public partial class TextWriterTraceListener : System.Diagnostics.TraceListener { public TextWriterTraceListener() { } public TextWriterTraceListener(System.IO.Stream stream) { } - public TextWriterTraceListener(System.IO.Stream stream, string name) { } + public TextWriterTraceListener(System.IO.Stream stream, string? name) { } public TextWriterTraceListener(System.IO.TextWriter writer) { } - public TextWriterTraceListener(System.IO.TextWriter writer, string name) { } - public TextWriterTraceListener(string fileName) { } - public TextWriterTraceListener(string fileName, string name) { } - public System.IO.TextWriter Writer { get { throw null; } set { } } + public TextWriterTraceListener(System.IO.TextWriter writer, string? name) { } + public TextWriterTraceListener(string? fileName) { } + public TextWriterTraceListener(string? fileName, string? name) { } + public System.IO.TextWriter? Writer { get { throw null; } set { } } public override void Close() { } protected override void Dispose(bool disposing) { } public override void Flush() { } - public override void Write(string message) { } - public override void WriteLine(string message) { } + public override void Write(string? message) { } + public override void WriteLine(string? message) { } } public partial class XmlWriterTraceListener : System.Diagnostics.TextWriterTraceListener { public XmlWriterTraceListener(System.IO.Stream stream) { } - public XmlWriterTraceListener(System.IO.Stream stream, string name) { } + public XmlWriterTraceListener(System.IO.Stream stream, string? name) { } public XmlWriterTraceListener(System.IO.TextWriter writer) { } - public XmlWriterTraceListener(System.IO.TextWriter writer, string name) { } - public XmlWriterTraceListener(string filename) { } - public XmlWriterTraceListener(string filename, string name) { } + public XmlWriterTraceListener(System.IO.TextWriter writer, string? name) { } + public XmlWriterTraceListener(string? filename) { } + public XmlWriterTraceListener(string? filename, string? name) { } public override void Close() { } - public override void Fail(string message, string detailMessage) { } - public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data) { } - public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, params object[] data) { } - public override void TraceEvent(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string message) { } - public override void TraceEvent(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string format, params object[] args) { } - public override void TraceTransfer(System.Diagnostics.TraceEventCache eventCache, string source, int id, string message, System.Guid relatedActivityId) { } - public override void Write(string message) { } - public override void WriteLine(string message) { } + public override void Fail(string? message, string? detailMessage) { } + public override void TraceData(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object? data) { } + public override void TraceData(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, params object?[]? data) { } + public override void TraceEvent(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string? message) { } + public override void TraceEvent(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string format, params object?[]? args) { } + public override void TraceTransfer(System.Diagnostics.TraceEventCache? eventCache, string source, int id, string? message, System.Guid relatedActivityId) { } + public override void Write(string? message) { } + public override void WriteLine(string? message) { } } } diff --git a/src/libraries/System.Diagnostics.TextWriterTraceListener/ref/System.Diagnostics.TextWriterTraceListener.csproj b/src/libraries/System.Diagnostics.TextWriterTraceListener/ref/System.Diagnostics.TextWriterTraceListener.csproj index 6e65ff61a3aa3..d83d56c13eb41 100644 --- a/src/libraries/System.Diagnostics.TextWriterTraceListener/ref/System.Diagnostics.TextWriterTraceListener.csproj +++ b/src/libraries/System.Diagnostics.TextWriterTraceListener/ref/System.Diagnostics.TextWriterTraceListener.csproj @@ -1,6 +1,7 @@ $(NetCoreAppCurrent) + enable diff --git a/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System.Diagnostics.TextWriterTraceListener.csproj b/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System.Diagnostics.TextWriterTraceListener.csproj index 4369501017e46..9649c64048ed1 100644 --- a/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System.Diagnostics.TextWriterTraceListener.csproj +++ b/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System.Diagnostics.TextWriterTraceListener.csproj @@ -3,6 +3,7 @@ System.Diagnostics.TextWriterTraceListener System.Diagnostics.TextWriterTraceListener $(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix + enable diff --git a/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/DelimitedListTraceListener.cs b/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/DelimitedListTraceListener.cs index e077ee9572b6b..e925f68415681 100644 --- a/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/DelimitedListTraceListener.cs +++ b/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/DelimitedListTraceListener.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.Text; using System.Globalization; using System.IO; @@ -12,6 +11,8 @@ namespace System.Diagnostics { public class DelimitedListTraceListener : TextWriterTraceListener { + private const string DelimiterKey = "delimiter"; + private string _delimiter = ";"; private string _secondaryDelim = ","; private bool _initializedDelim; @@ -20,7 +21,7 @@ public DelimitedListTraceListener(Stream stream) : base(stream) { } - public DelimitedListTraceListener(Stream stream, string name) : base(stream, name) + public DelimitedListTraceListener(Stream stream, string? name) : base(stream, name) { } @@ -28,7 +29,7 @@ public DelimitedListTraceListener(TextWriter writer) : base(writer) { } - public DelimitedListTraceListener(TextWriter writer, string name) : base(writer, name) + public DelimitedListTraceListener(TextWriter writer, string? name) : base(writer, name) { } @@ -36,7 +37,7 @@ public DelimitedListTraceListener(string fileName) : base(fileName) { } - public DelimitedListTraceListener(string fileName, string name) : base(fileName, name) + public DelimitedListTraceListener(string fileName, string? name) : base(fileName, name) { } @@ -48,9 +49,13 @@ public string Delimiter { if (!_initializedDelim) { - if (Attributes.ContainsKey("delimiter")) + if (Attributes.ContainsKey(DelimiterKey)) { - _delimiter = Attributes["delimiter"]; + string? delimiter = Attributes[DelimiterKey]; + if (!string.IsNullOrEmpty(delimiter)) + { + _delimiter = delimiter; + } } _initializedDelim = true; } @@ -80,9 +85,9 @@ public string Delimiter // base class method is protected internal but since its base class is in another assembly can't override it as protected internal because a CS0507 // warning would be hitted. - protected override string[] GetSupportedAttributes() => new string[] { "delimiter" }; + protected override string[] GetSupportedAttributes() => new string[] { DelimiterKey }; - public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args) + public override void TraceEvent(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, string format, params object?[]? args) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, format, args, null, null)) return; @@ -101,7 +106,7 @@ public override void TraceEvent(TraceEventCache eventCache, string source, Trace WriteFooter(eventCache); } - public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message) + public override void TraceEvent(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, string? message) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, message, null, null, null)) return; @@ -117,7 +122,7 @@ public override void TraceEvent(TraceEventCache eventCache, string source, Trace WriteFooter(eventCache); } - public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) + public override void TraceData(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, object? data) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null)) return; @@ -127,13 +132,13 @@ public override void TraceData(TraceEventCache eventCache, string source, TraceE // first a delimiter for the message Write(Delimiter); // Use get_Delimiter - WriteEscaped(data.ToString()); + WriteEscaped(data?.ToString()); Write(Delimiter); // Use get_Delimiter WriteFooter(eventCache); } - public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, params object[] data) + public override void TraceData(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, params object?[]? data) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, null, null, null, data)) return; @@ -149,7 +154,7 @@ public override void TraceData(TraceEventCache eventCache, string source, TraceE { if (i != 0) Write(_secondaryDelim); - WriteEscaped(data[i].ToString()); + WriteEscaped(data[i]?.ToString()); } } Write(Delimiter); // Use get_Delimiter @@ -169,7 +174,7 @@ private void WriteHeader(string source, TraceEventType eventType, int id) Write(Delimiter); // Use get_Delimiter } - private void WriteFooter(TraceEventCache eventCache) + private void WriteFooter(TraceEventCache? eventCache) { if (eventCache != null) { @@ -205,7 +210,7 @@ private void WriteFooter(TraceEventCache eventCache) WriteLine(""); } - private void WriteEscaped(string message) + private void WriteEscaped(string? message) { if (!string.IsNullOrEmpty(message)) { @@ -220,7 +225,7 @@ private void WriteStackEscaped(Stack stack) { StringBuilder sb = new StringBuilder("\""); bool first = true; - foreach (object obj in stack) + foreach (object? obj in stack) { if (!first) { @@ -231,7 +236,7 @@ private void WriteStackEscaped(Stack stack) first = false; } - string operation = obj.ToString(); + string? operation = obj?.ToString(); EscapeMessage(operation, sb); } @@ -239,18 +244,21 @@ private void WriteStackEscaped(Stack stack) Write(sb.ToString()); } - private void EscapeMessage(string message, StringBuilder sb) + private void EscapeMessage(string? message, StringBuilder sb) { - int index; - int lastindex = 0; - while ((index = message.IndexOf('"', lastindex)) != -1) + if (!string.IsNullOrEmpty(message)) { - sb.Append(message, lastindex, index - lastindex); - sb.Append("\"\""); - lastindex = index + 1; - } + int index; + int lastindex = 0; + while ((index = message.IndexOf('"', lastindex)) != -1) + { + sb.Append(message, lastindex, index - lastindex); + sb.Append("\"\""); + lastindex = index + 1; + } - sb.Append(message, lastindex, message.Length - lastindex); + sb.Append(message, lastindex, message.Length - lastindex); + } } } } diff --git a/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/TextWriterTraceListener.cs b/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/TextWriterTraceListener.cs index 664e46f02e384..9cbf9753fb109 100644 --- a/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/TextWriterTraceListener.cs +++ b/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/TextWriterTraceListener.cs @@ -2,10 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.IO; using System.Text; -using System.Runtime.InteropServices; namespace System.Diagnostics { @@ -16,8 +14,8 @@ namespace System.Diagnostics /// public class TextWriterTraceListener : TraceListener { - internal TextWriter _writer; - private string _fileName; + internal TextWriter? _writer; + private string? _fileName; /// /// Initializes a new instance of the class with @@ -41,7 +39,7 @@ public TextWriterTraceListener(Stream stream) /// Initializes a new instance of the class with the /// specified name and using the stream as the recipient of the debugging and tracing output. /// - public TextWriterTraceListener(Stream stream, string name) + public TextWriterTraceListener(Stream stream, string? name) : base(name) { if (stream == null) throw new ArgumentNullException(nameof(stream)); @@ -63,7 +61,7 @@ public TextWriterTraceListener(TextWriter writer) /// debugging /// output. /// - public TextWriterTraceListener(TextWriter writer, string name) + public TextWriterTraceListener(TextWriter writer, string? name) : base(name) { if (writer == null) throw new ArgumentNullException(nameof(writer)); @@ -74,7 +72,7 @@ public TextWriterTraceListener(TextWriter writer, string name) /// Initializes a new instance of the class with the /// specified file name. /// - public TextWriterTraceListener(string fileName) + public TextWriterTraceListener(string? fileName) { _fileName = fileName; } @@ -83,7 +81,7 @@ public TextWriterTraceListener(string fileName) /// Initializes a new instance of the class with the /// specified name and the specified file name. /// - public TextWriterTraceListener(string fileName, string name) + public TextWriterTraceListener(string? fileName, string? name) : base(name) { _fileName = fileName; @@ -93,7 +91,7 @@ public TextWriterTraceListener(string fileName, string name) /// Indicates the text writer that receives the tracing /// or debugging output. /// - public TextWriter Writer + public TextWriter? Writer { get { @@ -163,7 +161,7 @@ public override void Flush() /// Writes a message /// to this instance's . /// - public override void Write(string message) + public override void Write(string? message) { EnsureWriter(); if (_writer != null) @@ -182,7 +180,7 @@ public override void Write(string message) /// to this instance's followed by a line terminator. The /// default line terminator is a carriage return followed by a line feed (\r\n). /// - public override void WriteLine(string message) + public override void WriteLine(string? message) { EnsureWriter(); if (_writer != null) @@ -209,11 +207,9 @@ private static Encoding GetEncodingWithFallback(Encoding encoding) internal void EnsureWriter() { - bool success = true; - if (_writer == null) { - success = false; + bool success = false; if (_fileName == null) return; @@ -232,7 +228,7 @@ internal void EnsureWriter() // IO errors, we will prefix the file name with a unique GUID value // and try one more time string fullPath = Path.GetFullPath(_fileName); - string dirPath = Path.GetDirectoryName(fullPath); + string dirPath = Path.GetDirectoryName(fullPath)!; string fileNameOnly = Path.GetFileName(fullPath); for (int i = 0; i < 2; i++) diff --git a/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/XmlWriterTraceListener.cs b/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/XmlWriterTraceListener.cs index 5d17aeedc2092..f9ec5d0160783 100644 --- a/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/XmlWriterTraceListener.cs +++ b/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/XmlWriterTraceListener.cs @@ -17,15 +17,15 @@ public class XmlWriterTraceListener : TextWriterTraceListener private const string FixedHeader = ""; private readonly string _machineName = Environment.MachineName; - private StringBuilder _strBldr = null; - private XmlTextWriter _xmlBlobWriter = null; + private StringBuilder? _strBldr; + private XmlTextWriter? _xmlBlobWriter; public XmlWriterTraceListener(Stream stream) : base(stream) { } - public XmlWriterTraceListener(Stream stream, string name) + public XmlWriterTraceListener(Stream stream, string? name) : base(stream, name) { } @@ -35,37 +35,38 @@ public XmlWriterTraceListener(TextWriter writer) { } - public XmlWriterTraceListener(TextWriter writer, string name) + public XmlWriterTraceListener(TextWriter writer, string? name) : base(writer, name) { } [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] - public XmlWriterTraceListener(string filename) + public XmlWriterTraceListener(string? filename) : base(filename) { } [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] - public XmlWriterTraceListener(string filename, string name) + public XmlWriterTraceListener(string? filename, string? name) : base(filename, name) { } - public override void Write(string message) + public override void Write(string? message) { WriteLine(message); } - public override void WriteLine(string message) + public override void WriteLine(string? message) { TraceEvent(null, SR.TraceAsTraceSource, TraceEventType.Information, 0, message); } - public override void Fail(string message, string detailMessage) + public override void Fail(string? message, string? detailMessage) { + message ??= string.Empty; int length = detailMessage != null ? message.Length + 1 + detailMessage.Length : message.Length; TraceEvent(null, SR.TraceAsTraceSource, TraceEventType.Error, 0, string.Create(length, (message, detailMessage), (dst, v) => @@ -83,7 +84,7 @@ public override void Fail(string message, string detailMessage) })); } - public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args) + public override void TraceEvent(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, string format, params object?[]? args) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, format, args, null, null)) return; @@ -93,7 +94,7 @@ public override void TraceEvent(TraceEventCache eventCache, string source, Trace WriteFooter(eventCache); } - public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message) + public override void TraceEvent(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, string? message) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, message, null, null, null)) return; @@ -103,7 +104,7 @@ public override void TraceEvent(TraceEventCache eventCache, string source, Trace WriteFooter(eventCache); } - public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) + public override void TraceData(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, object? data) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null)) return; @@ -122,7 +123,7 @@ public override void TraceData(TraceEventCache eventCache, string source, TraceE WriteFooter(eventCache); } - public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, params object[] data) + public override void TraceData(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, params object?[]? data) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, null, null, null, data)) return; @@ -135,7 +136,7 @@ public override void TraceData(TraceEventCache eventCache, string source, TraceE { InternalWrite(""); if (data[i] != null) - WriteData(data[i]); + WriteData(data[i]!); InternalWrite(""); } } @@ -167,7 +168,7 @@ private void WriteData(object data) { // Rewind the blob to point to the root, this is needed to support multiple XMLTL in one TraceData call xmlBlob.MoveToRoot(); - _xmlBlobWriter.WriteNode(xmlBlob, false); + _xmlBlobWriter!.WriteNode(xmlBlob, false); InternalWrite(_strBldr.ToString()); } catch (Exception) @@ -185,7 +186,7 @@ public override void Close() _strBldr = null; } - public override void TraceTransfer(TraceEventCache eventCache, string source, int id, string message, Guid relatedActivityId) + public override void TraceTransfer(TraceEventCache? eventCache, string source, int id, string? message, Guid relatedActivityId) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, TraceEventType.Transfer, id, message, null, null, null)) return; @@ -195,7 +196,7 @@ public override void TraceTransfer(TraceEventCache eventCache, string source, in WriteFooter(eventCache); } - private void WriteHeader(string source, TraceEventType eventType, int id, TraceEventCache eventCache, Guid relatedActivityId) + private void WriteHeader(string source, TraceEventType eventType, int id, TraceEventCache? eventCache, Guid relatedActivityId) { WriteStartHeader(source, eventType, id, eventCache); InternalWrite("\" RelatedActivityID=\""); @@ -203,13 +204,13 @@ private void WriteHeader(string source, TraceEventType eventType, int id, TraceE WriteEndHeader(); } - private void WriteHeader(string source, TraceEventType eventType, int id, TraceEventCache eventCache) + private void WriteHeader(string source, TraceEventType eventType, int id, TraceEventCache? eventCache) { WriteStartHeader(source, eventType, id, eventCache); WriteEndHeader(); } - private void WriteStartHeader(string source, TraceEventType eventType, int id, TraceEventCache eventCache) + private void WriteStartHeader(string source, TraceEventType eventType, int id, TraceEventCache? eventCache) { InternalWrite(FixedHeader); @@ -275,7 +276,7 @@ private void WriteEndHeader() InternalWrite(""); } - private void WriteFooter(TraceEventCache eventCache) + private void WriteFooter(TraceEventCache? eventCache) { bool writeLogicalOps = IsEnabled(TraceOptions.LogicalOperationStack); bool writeCallstack = IsEnabled(TraceOptions.Callstack); @@ -290,10 +291,10 @@ private void WriteFooter(TraceEventCache eventCache) Stack s = eventCache.LogicalOperationStack; - foreach (object correlationId in s) + foreach (object? correlationId in s) { InternalWrite(""); - WriteEscaped(correlationId.ToString()); + WriteEscaped(correlationId?.ToString()); InternalWrite(""); } InternalWrite(""); @@ -316,7 +317,7 @@ private void WriteFooter(TraceEventCache eventCache) InternalWrite(""); } - private void WriteEscaped(string str) + private void WriteEscaped(string? str) { if (string.IsNullOrEmpty(str)) return; @@ -366,12 +367,10 @@ private void WriteEscaped(string str) InternalWrite(str.Substring(lastIndex, str.Length - lastIndex)); } - private void InternalWrite(string message) + private void InternalWrite(string? message) { EnsureWriter(); - - if (_writer != null) - _writer.Write(message); + _writer?.Write(message); } } } diff --git a/src/libraries/System.Diagnostics.TraceSource/ref/System.Diagnostics.TraceSource.cs b/src/libraries/System.Diagnostics.TraceSource/ref/System.Diagnostics.TraceSource.cs index da867700460dd..05d59205b35dd 100644 --- a/src/libraries/System.Diagnostics.TraceSource/ref/System.Diagnostics.TraceSource.cs +++ b/src/libraries/System.Diagnostics.TraceSource/ref/System.Diagnostics.TraceSource.cs @@ -207,12 +207,13 @@ public enum TraceLevel public abstract partial class TraceListener : System.MarshalByRefObject, System.IDisposable { protected TraceListener() { } - protected TraceListener(string name) { } + protected TraceListener(string? name) { } public System.Collections.Specialized.StringDictionary Attributes { get { throw null; } } public System.Diagnostics.TraceFilter? Filter { get { throw null; } set { } } public int IndentLevel { get { throw null; } set { } } public int IndentSize { get { throw null; } set { } } public virtual bool IsThreadSafe { get { throw null; } } + [System.Diagnostics.CodeAnalysis.AllowNull] public virtual string Name { get { throw null; } set { } } protected bool NeedIndent { get { throw null; } set { } } public System.Diagnostics.TraceOptions TraceOutputOptions { get { throw null; } set { } } diff --git a/src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/TraceListener.cs b/src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/TraceListener.cs index 68a5e45f5c9fa..840a35fdf15fe 100644 --- a/src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/TraceListener.cs +++ b/src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/TraceListener.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Diagnostics.CodeAnalysis; using System.Text; using System.Globalization; using System.Runtime.InteropServices; @@ -37,7 +38,7 @@ protected TraceListener() /// Initializes a new instance of the class using the specified name as the /// listener. /// - protected TraceListener(string name) + protected TraceListener(string? name) { _listenerName = name; } @@ -55,9 +56,10 @@ public StringDictionary Attributes /// /// Gets or sets a name for this . /// + [AllowNull] public virtual string Name { - get { return (_listenerName == null) ? "" : _listenerName; } + get { return _listenerName ?? ""; } set { _listenerName = value; } }