|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Diagnostics.CodeAnalysis; |
3 | 4 | using System.IO;
|
4 | 5 | using System.Linq;
|
5 | 6 | using System.Xml;
|
@@ -72,6 +73,8 @@ public Log4NetTextFormatter(Action<Log4NetTextFormatterOptionsBuilder>? configur
|
72 | 73 | /// <param name="output">The output.</param>
|
73 | 74 | public void Format(LogEvent logEvent, TextWriter output)
|
74 | 75 | {
|
| 76 | + if (logEvent == null) throw new ArgumentNullException(nameof(logEvent)); |
| 77 | + if (output == null) throw new ArgumentNullException(nameof(output)); |
75 | 78 | using var writer = XmlWriter.Create(output, _options.XmlWriterSettings);
|
76 | 79 | WriteEvent(logEvent, writer);
|
77 | 80 | writer.Flush();
|
@@ -170,6 +173,7 @@ private static string LogLevel(LogEventLevel level)
|
170 | 173 | /// <param name="properties">The collection of properties to write.</param>
|
171 | 174 | /// <param name="machineNameProperty">The machine name property to write or <see langref="null"/> if doesn't exist.</param>
|
172 | 175 | /// <remarks>https://github.com/apache/logging-log4net/blob/rel/2.0.8/src/Layout/XmlLayout.cs#L262-L286</remarks>
|
| 176 | + [SuppressMessage("Microsoft.Design", "CA1031", Justification = "Protecting from user-provided code which might throw anything")] |
173 | 177 | private void WriteProperties(LogEvent logEvent, XmlWriter writer, IEnumerable<KeyValuePair<string, LogEventPropertyValue>> properties, LogEventPropertyValue? machineNameProperty)
|
174 | 178 | {
|
175 | 179 | WriteStartElement(writer, "properties");
|
@@ -205,7 +209,7 @@ private void WriteProperties(LogEvent logEvent, XmlWriter writer, IEnumerable<Ke
|
205 | 209 | /// <returns>A string representation of the <paramref name="value"/>.</returns>
|
206 | 210 | private string RenderValue(LogEventPropertyValue value)
|
207 | 211 | {
|
208 |
| - var valueWriter = new StringWriter(); |
| 212 | + using var valueWriter = new StringWriter(); |
209 | 213 | // The "l" format specifier switches off quoting of strings, see https://github.com/serilog/serilog/wiki/Formatting-Output#formatting-plain-text
|
210 | 214 | value.Render(valueWriter, value is ScalarValue scalarValue && scalarValue.Value is string ? "l" : null, _options.FormatProvider);
|
211 | 215 | return valueWriter.ToString();
|
@@ -328,6 +332,7 @@ private void WriteMessage(LogEvent logEvent, XmlWriter writer)
|
328 | 332 | /// <param name="logEvent">The log event.</param>
|
329 | 333 | /// <param name="writer">The XML writer.</param>
|
330 | 334 | /// <remarks>https://github.com/apache/logging-log4net/blob/rel/2.0.8/src/Layout/XmlLayout.cs#L288-L295</remarks>
|
| 335 | + [SuppressMessage("Microsoft.Design", "CA1031", Justification = "Protecting from user-provided code which might throw anything")] |
331 | 336 | private void WriteException(LogEvent logEvent, XmlWriter writer)
|
332 | 337 | {
|
333 | 338 | var exception = logEvent.Exception;
|
|
0 commit comments