From 3941383c2fd7840726069b8b4e35b5d7a1fc73f5 Mon Sep 17 00:00:00 2001 From: Tsar Nikolay Date: Tue, 8 Sep 2020 13:58:44 +0500 Subject: [PATCH] Fix CA2000, CA2237, CA3075 --- src/log4net/Appender/AdoNetAppender.cs | 2 +- src/log4net/Appender/FileAppender.cs | 25 +- src/log4net/Appender/SmtpAppender.cs | 260 +++---- src/log4net/Config/XmlConfigurator.cs | 638 +++++++++--------- src/log4net/Layout/Layout2RawLayoutAdapter.cs | 2 +- src/log4net/Layout/LayoutSkeleton.cs | 182 ++--- src/log4net/ObjectRenderer/RendererMap.cs | 4 +- src/log4net/Util/PatternString.cs | 2 +- 8 files changed, 575 insertions(+), 540 deletions(-) diff --git a/src/log4net/Appender/AdoNetAppender.cs b/src/log4net/Appender/AdoNetAppender.cs index a509cc17..72a5a762 100644 --- a/src/log4net/Appender/AdoNetAppender.cs +++ b/src/log4net/Appender/AdoNetAppender.cs @@ -617,7 +617,7 @@ virtual protected string GetLogStatement(LoggingEvent logEvent) } else { - StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); + using StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); Layout.Format(writer, logEvent); return writer.ToString(); } diff --git a/src/log4net/Appender/FileAppender.cs b/src/log4net/Appender/FileAppender.cs index e283f49e..f0c2bc1f 100644 --- a/src/log4net/Appender/FileAppender.cs +++ b/src/log4net/Appender/FileAppender.cs @@ -19,6 +19,9 @@ using System; using System.IO; +#if !NETCF && !NETSTANDARD1_3 +using System.Runtime.Serialization; +#endif using System.Text; using System.Threading; using log4net.Util; @@ -138,12 +141,29 @@ public class FileAppender : TextWriterAppender /// private sealed class LockingStream : Stream, IDisposable { +#if !NETCR + [Serializable] +#endif public sealed class LockStateException : LogException { public LockStateException(string message) : base(message) { } + + public LockStateException() + { + } + + public LockStateException(string message, Exception innerException) : base(message, innerException) + { + } + +#if !NETCR && !NETSTANDARD1_3 + private LockStateException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } +#endif } private Stream m_realStream = null; @@ -1409,7 +1429,10 @@ virtual protected void OpenFile(string fileName, bool append) /// virtual protected void SetQWForFiles(Stream fileStream) { - SetQWForFiles(new StreamWriter(fileStream, m_encoding)); +#pragma warning disable CA2000 // Dispose objects before losing scope + StreamWriter writer = new StreamWriter(fileStream, m_encoding); +#pragma warning restore CA2000 // Dispose objects before losing scope + SetQWForFiles(writer); } /// diff --git a/src/log4net/Appender/SmtpAppender.cs b/src/log4net/Appender/SmtpAppender.cs index d19ad90a..bbe8bd51 100644 --- a/src/log4net/Appender/SmtpAppender.cs +++ b/src/log4net/Appender/SmtpAppender.cs @@ -92,20 +92,20 @@ public SmtpAppender() /// Gets or sets a comma- or semicolon-delimited list of recipient e-mail addresses (use semicolon on .NET 1.1 and comma for later versions). /// /// - /// - /// For .NET 1.1 (System.Web.Mail): A semicolon-delimited list of e-mail addresses. - /// - /// - /// For .NET 2.0 (System.Net.Mail): A comma-delimited list of e-mail addresses. - /// + /// + /// For .NET 1.1 (System.Web.Mail): A semicolon-delimited list of e-mail addresses. + /// + /// + /// For .NET 2.0 (System.Net.Mail): A comma-delimited list of e-mail addresses. + /// /// /// - /// - /// For .NET 1.1 (System.Web.Mail): A semicolon-delimited list of e-mail addresses. - /// - /// - /// For .NET 2.0 (System.Net.Mail): A comma-delimited list of e-mail addresses. - /// + /// + /// For .NET 1.1 (System.Web.Mail): A semicolon-delimited list of e-mail addresses. + /// + /// + /// For .NET 2.0 (System.Net.Mail): A comma-delimited list of e-mail addresses. + /// /// public string To { @@ -113,49 +113,49 @@ public string To set { m_to = MaybeTrimSeparators(value); } } - /// - /// Gets or sets a comma- or semicolon-delimited list of recipient e-mail addresses - /// that will be carbon copied (use semicolon on .NET 1.1 and comma for later versions). - /// - /// - /// - /// For .NET 1.1 (System.Web.Mail): A semicolon-delimited list of e-mail addresses. - /// - /// - /// For .NET 2.0 (System.Net.Mail): A comma-delimited list of e-mail addresses. - /// - /// - /// - /// - /// For .NET 1.1 (System.Web.Mail): A semicolon-delimited list of e-mail addresses. - /// - /// - /// For .NET 2.0 (System.Net.Mail): A comma-delimited list of e-mail addresses. - /// - /// - public string Cc - { - get { return m_cc; } - set { m_cc = MaybeTrimSeparators(value); } - } - - /// - /// Gets or sets a semicolon-delimited list of recipient e-mail addresses - /// that will be blind carbon copied. - /// - /// - /// A semicolon-delimited list of e-mail addresses. - /// - /// - /// - /// A semicolon-delimited list of recipient e-mail addresses. - /// - /// - public string Bcc - { - get { return m_bcc; } - set { m_bcc = MaybeTrimSeparators(value); } - } + /// + /// Gets or sets a comma- or semicolon-delimited list of recipient e-mail addresses + /// that will be carbon copied (use semicolon on .NET 1.1 and comma for later versions). + /// + /// + /// + /// For .NET 1.1 (System.Web.Mail): A semicolon-delimited list of e-mail addresses. + /// + /// + /// For .NET 2.0 (System.Net.Mail): A comma-delimited list of e-mail addresses. + /// + /// + /// + /// + /// For .NET 1.1 (System.Web.Mail): A semicolon-delimited list of e-mail addresses. + /// + /// + /// For .NET 2.0 (System.Net.Mail): A comma-delimited list of e-mail addresses. + /// + /// + public string Cc + { + get { return m_cc; } + set { m_cc = MaybeTrimSeparators(value); } + } + + /// + /// Gets or sets a semicolon-delimited list of recipient e-mail addresses + /// that will be blind carbon copied. + /// + /// + /// A semicolon-delimited list of e-mail addresses. + /// + /// + /// + /// A semicolon-delimited list of recipient e-mail addresses. + /// + /// + public string Bcc + { + get { return m_bcc; } + set { m_bcc = MaybeTrimSeparators(value); } + } /// /// Gets or sets the e-mail address of the sender. @@ -324,29 +324,29 @@ public MailPriority Priority } #if NET_2_0 || MONO_2_0 || NETSTANDARD2_0 - /// - /// Enable or disable use of SSL when sending e-mail message - /// - /// - /// This is available on MS .NET 2.0 runtime and higher - /// - public bool EnableSsl - { - get { return m_enableSsl; } - set { m_enableSsl = value; } - } - - /// - /// Gets or sets the reply-to e-mail address. - /// - /// - /// This is available on MS .NET 2.0 runtime and higher - /// - public string ReplyTo - { - get { return m_replyTo; } - set { m_replyTo = value; } - } + /// + /// Enable or disable use of SSL when sending e-mail message + /// + /// + /// This is available on MS .NET 2.0 runtime and higher + /// + public bool EnableSsl + { + get { return m_enableSsl; } + set { m_enableSsl = value; } + } + + /// + /// Gets or sets the reply-to e-mail address. + /// + /// + /// This is available on MS .NET 2.0 runtime and higher + /// + public string ReplyTo + { + get { return m_replyTo; } + set { m_replyTo = value; } + } #endif /// @@ -387,7 +387,7 @@ override protected void SendBuffer(LoggingEvent[] events) // appender. This frees us from needing to synchronize again. try { - StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); + using StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); string t = Layout.Header; if (t != null) @@ -449,14 +449,18 @@ virtual protected void SendEmail(string messageBody) // The old API is deprecated. // Create and configure the smtp client +#if NET_4_0 || MONO_4_0 || NETSTANDARD2_0 + using SmtpClient smtpClient = new SmtpClient(); +#else SmtpClient smtpClient = new SmtpClient(); +#endif if (!String.IsNullOrEmpty(m_smtpHost)) { smtpClient.Host = m_smtpHost; } smtpClient.Port = m_port; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; - smtpClient.EnableSsl = m_enableSsl; + smtpClient.EnableSsl = m_enableSsl; if (m_authentication == SmtpAuthentication.Basic) { @@ -469,38 +473,38 @@ virtual protected void SendEmail(string messageBody) smtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; } - using (MailMessage mailMessage = new MailMessage()) - { - mailMessage.Body = messageBody; + using (MailMessage mailMessage = new MailMessage()) + { + mailMessage.Body = messageBody; mailMessage.BodyEncoding = m_bodyEncoding; - mailMessage.From = new MailAddress(m_from); - mailMessage.To.Add(m_to); - if (!String.IsNullOrEmpty(m_cc)) - { - mailMessage.CC.Add(m_cc); - } - if (!String.IsNullOrEmpty(m_bcc)) - { - mailMessage.Bcc.Add(m_bcc); - } - if (!String.IsNullOrEmpty(m_replyTo)) - { - // .NET 4.0 warning CS0618: 'System.Net.Mail.MailMessage.ReplyTo' is obsolete: - // 'ReplyTo is obsoleted for this type. Please use ReplyToList instead which can accept multiple addresses. http://go.microsoft.com/fwlink/?linkid=14202' + mailMessage.From = new MailAddress(m_from); + mailMessage.To.Add(m_to); + if (!String.IsNullOrEmpty(m_cc)) + { + mailMessage.CC.Add(m_cc); + } + if (!String.IsNullOrEmpty(m_bcc)) + { + mailMessage.Bcc.Add(m_bcc); + } + if (!String.IsNullOrEmpty(m_replyTo)) + { + // .NET 4.0 warning CS0618: 'System.Net.Mail.MailMessage.ReplyTo' is obsolete: + // 'ReplyTo is obsoleted for this type. Please use ReplyToList instead which can accept multiple addresses. http://go.microsoft.com/fwlink/?linkid=14202' #if !NET_4_0 && !MONO_4_0 && !NETSTANDARD2_0 - mailMessage.ReplyTo = new MailAddress(m_replyTo); + mailMessage.ReplyTo = new MailAddress(m_replyTo); #else - mailMessage.ReplyToList.Add(new MailAddress(m_replyTo)); + mailMessage.ReplyToList.Add(new MailAddress(m_replyTo)); #endif - } - mailMessage.Subject = m_subject; + } + mailMessage.Subject = m_subject; mailMessage.SubjectEncoding = m_subjectEncoding; - mailMessage.Priority = m_mailPriority; + mailMessage.Priority = m_mailPriority; - // TODO: Consider using SendAsync to send the message without blocking. This would be a change in - // behaviour compared to .NET 1.x. We would need a SendCompletedCallback to log errors. - smtpClient.Send(mailMessage); - } + // TODO: Consider using SendAsync to send the message without blocking. This would be a change in + // behaviour compared to .NET 1.x. We would need a SendCompletedCallback to log errors. + smtpClient.Send(mailMessage); + } #else // .NET 1.x uses the System.Web.Mail API for sending Mail @@ -509,14 +513,14 @@ virtual protected void SendEmail(string messageBody) mailMessage.BodyEncoding = m_bodyEncoding; mailMessage.From = m_from; mailMessage.To = m_to; - if (m_cc != null && m_cc.Length > 0) - { - mailMessage.Cc = m_cc; - } - if (m_bcc != null && m_bcc.Length > 0) - { - mailMessage.Bcc = m_bcc; - } + if (m_cc != null && m_cc.Length > 0) + { + mailMessage.Cc = m_cc; + } + if (m_bcc != null && m_bcc.Length > 0) + { + mailMessage.Bcc = m_bcc; + } mailMessage.Subject = m_subject; #if !MONO && !NET_1_0 && !NET_1_1 && !CLI_1_0 mailMessage.SubjectEncoding = m_subjectEncoding; @@ -585,8 +589,8 @@ virtual protected void SendEmail(string messageBody) #region Private Instance Fields private string m_to; - private string m_cc; - private string m_bcc; + private string m_cc; + private string m_bcc; private string m_from; private string m_subject; private string m_smtpHost; @@ -604,8 +608,8 @@ virtual protected void SendEmail(string messageBody) private MailPriority m_mailPriority = MailPriority.Normal; #if NET_2_0 || MONO_2_0 || NETSTANDARD2_0 - private bool m_enableSsl = false; - private string m_replyTo; + private bool m_enableSsl = false; + private string m_replyTo; #endif #endregion // Private Instance Fields @@ -646,19 +650,19 @@ public enum SmtpAuthentication #endregion // SmtpAuthentication Enum - private static readonly char[] ADDRESS_DELIMITERS = new char[] { ',', ';' }; - - /// - /// trims leading and trailing commas or semicolons - /// - private static string MaybeTrimSeparators(string s) { + private static readonly char[] ADDRESS_DELIMITERS = new char[] { ',', ';' }; + + /// + /// trims leading and trailing commas or semicolons + /// + private static string MaybeTrimSeparators(string s) { #if NET_2_0 || MONO_2_0 || NETSTANDARD2_0 - return string.IsNullOrEmpty(s) ? s : s.Trim(ADDRESS_DELIMITERS); + return string.IsNullOrEmpty(s) ? s : s.Trim(ADDRESS_DELIMITERS); #else - return s != null && s.Length > 0 ? s : s.Trim(ADDRESS_DELIMITERS); + return s != null && s.Length > 0 ? s : s.Trim(ADDRESS_DELIMITERS); #endif - } - } + } + } } #endif // !NETCF && !SSCLI diff --git a/src/log4net/Config/XmlConfigurator.cs b/src/log4net/Config/XmlConfigurator.cs index 02795fd1..cd9ebfea 100644 --- a/src/log4net/Config/XmlConfigurator.cs +++ b/src/log4net/Config/XmlConfigurator.cs @@ -49,8 +49,8 @@ public sealed class XmlConfigurator /// /// Private constructor /// - private XmlConfigurator() - { + private XmlConfigurator() + { } #endregion Protected Instance Constructors @@ -58,26 +58,26 @@ private XmlConfigurator() #region Configure static methods #if !NETCF - /// - /// Automatically configures the using settings - /// stored in the application's configuration file. - /// - /// - /// - /// Each application has a configuration file. This has the - /// same name as the application with '.config' appended. - /// This file is XML and calling this function prompts the - /// configurator to look in that file for a section called - /// log4net that contains the configuration data. - /// - /// - /// To use this method to configure log4net you must specify - /// the section - /// handler for the log4net configuration section. See the - /// for an example. - /// - /// - /// The repository to configure. + /// + /// Automatically configures the using settings + /// stored in the application's configuration file. + /// + /// + /// + /// Each application has a configuration file. This has the + /// same name as the application with '.config' appended. + /// This file is XML and calling this function prompts the + /// configurator to look in that file for a section called + /// log4net that contains the configuration data. + /// + /// + /// To use this method to configure log4net you must specify + /// the section + /// handler for the log4net configuration section. See the + /// for an example. + /// + /// + /// The repository to configure. #else /// /// Automatically configures the using settings @@ -94,21 +94,21 @@ private XmlConfigurator() /// /// The repository to configure. #endif - static public ICollection Configure(ILoggerRepository repository) - { - ArrayList configurationMessages = new ArrayList(); + static public ICollection Configure(ILoggerRepository repository) + { + ArrayList configurationMessages = new ArrayList(); - using (new LogLog.LogReceivedAdapter(configurationMessages)) - { - InternalConfigure(repository); - } + using (new LogLog.LogReceivedAdapter(configurationMessages)) + { + InternalConfigure(repository); + } - repository.ConfigurationMessages = configurationMessages; + repository.ConfigurationMessages = configurationMessages; - return configurationMessages; - } + return configurationMessages; + } - static private void InternalConfigure(ILoggerRepository repository) + static private void InternalConfigure(ILoggerRepository repository) { LogLog.Debug(declaringType, "configuring repository [" + repository.Name + "] using .config file section"); @@ -194,10 +194,10 @@ static private void InternalConfigure(ILoggerRepository repository) /// /// #endif - static public ICollection Configure() - { - return Configure(LogManager.GetRepository(Assembly.GetCallingAssembly())); - } + static public ICollection Configure() + { + return Configure(LogManager.GetRepository(Assembly.GetCallingAssembly())); + } /// /// Configures log4net using a log4net element @@ -209,20 +209,20 @@ static public ICollection Configure() /// /// /// The element to parse. - static public ICollection Configure(XmlElement element) + static public ICollection Configure(XmlElement element) { - ArrayList configurationMessages = new ArrayList(); + ArrayList configurationMessages = new ArrayList(); - ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly()); + ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly()); - using (new LogLog.LogReceivedAdapter(configurationMessages)) - { - InternalConfigureFromXml(repository, element); - } + using (new LogLog.LogReceivedAdapter(configurationMessages)) + { + InternalConfigureFromXml(repository, element); + } - repository.ConfigurationMessages = configurationMessages; + repository.ConfigurationMessages = configurationMessages; - return configurationMessages; + return configurationMessages; } #if !NETCF @@ -314,14 +314,14 @@ static public ICollection Configure(XmlElement element) #endif static public ICollection Configure(FileInfo configFile) { - ArrayList configurationMessages = new ArrayList(); + ArrayList configurationMessages = new ArrayList(); - using (new LogLog.LogReceivedAdapter(configurationMessages)) - { - InternalConfigure(LogManager.GetRepository(Assembly.GetCallingAssembly()), configFile); - } + using (new LogLog.LogReceivedAdapter(configurationMessages)) + { + InternalConfigure(LogManager.GetRepository(Assembly.GetCallingAssembly()), configFile); + } - return configurationMessages; + return configurationMessages; } /// @@ -340,17 +340,17 @@ static public ICollection Configure(FileInfo configFile) /// static public ICollection Configure(Uri configUri) { - ArrayList configurationMessages = new ArrayList(); + ArrayList configurationMessages = new ArrayList(); - ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly()); - using (new LogLog.LogReceivedAdapter(configurationMessages)) - { - InternalConfigure(repository, configUri); - } + ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly()); + using (new LogLog.LogReceivedAdapter(configurationMessages)) + { + InternalConfigure(repository, configUri); + } - repository.ConfigurationMessages = configurationMessages; + repository.ConfigurationMessages = configurationMessages; - return configurationMessages; + return configurationMessages; } /// @@ -369,99 +369,99 @@ static public ICollection Configure(Uri configUri) /// static public ICollection Configure(Stream configStream) { - ArrayList configurationMessages = new ArrayList(); + ArrayList configurationMessages = new ArrayList(); - ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly()); - using (new LogLog.LogReceivedAdapter(configurationMessages)) - { - InternalConfigure(repository, configStream); - } + ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly()); + using (new LogLog.LogReceivedAdapter(configurationMessages)) + { + InternalConfigure(repository, configStream); + } - repository.ConfigurationMessages = configurationMessages; + repository.ConfigurationMessages = configurationMessages; - return configurationMessages; + return configurationMessages; } #endif // !NETSTANDARD1_3 - /// - /// Configures the using the specified XML - /// element. - /// - /// - /// Loads the log4net configuration from the XML element - /// supplied as . - /// - /// The repository to configure. - /// The element to parse. - static public ICollection Configure(ILoggerRepository repository, XmlElement element) - { - ArrayList configurationMessages = new ArrayList(); + /// + /// Configures the using the specified XML + /// element. + /// + /// + /// Loads the log4net configuration from the XML element + /// supplied as . + /// + /// The repository to configure. + /// The element to parse. + static public ICollection Configure(ILoggerRepository repository, XmlElement element) + { + ArrayList configurationMessages = new ArrayList(); - using (new LogLog.LogReceivedAdapter(configurationMessages)) - { - LogLog.Debug(declaringType, "configuring repository [" + repository.Name + "] using XML element"); + using (new LogLog.LogReceivedAdapter(configurationMessages)) + { + LogLog.Debug(declaringType, "configuring repository [" + repository.Name + "] using XML element"); - InternalConfigureFromXml(repository, element); - } + InternalConfigureFromXml(repository, element); + } - repository.ConfigurationMessages = configurationMessages; + repository.ConfigurationMessages = configurationMessages; - return configurationMessages; - } + return configurationMessages; + } #if !NETCF - /// - /// Configures the using the specified configuration - /// file. - /// - /// The repository to configure. - /// The XML file to load the configuration from. - /// - /// - /// The configuration file must be valid XML. It must contain - /// at least one element called log4net that holds - /// the configuration data. - /// - /// - /// The log4net configuration file can possible be specified in the application's - /// configuration file (either MyAppName.exe.config for a - /// normal application on Web.config for an ASP.NET application). - /// - /// - /// The first element matching <configuration> will be read as the - /// configuration. If this file is also a .NET .config file then you must specify - /// a configuration section for the log4net element otherwise .NET will - /// complain. Set the type for the section handler to , for example: - /// - /// - ///
- /// - /// - /// - /// - /// The following example configures log4net using a configuration file, of which the - /// location is stored in the application's configuration file : - /// - /// - /// using log4net.Config; - /// using System.IO; - /// using System.Configuration; - /// - /// ... - /// - /// XmlConfigurator.Configure(new FileInfo(ConfigurationSettings.AppSettings["log4net-config-file"])); - /// - /// - /// In the .config file, the path to the log4net can be specified like this : - /// - /// - /// - /// - /// - /// - /// - /// - /// + /// + /// Configures the using the specified configuration + /// file. + /// + /// The repository to configure. + /// The XML file to load the configuration from. + /// + /// + /// The configuration file must be valid XML. It must contain + /// at least one element called log4net that holds + /// the configuration data. + /// + /// + /// The log4net configuration file can possible be specified in the application's + /// configuration file (either MyAppName.exe.config for a + /// normal application on Web.config for an ASP.NET application). + /// + /// + /// The first element matching <configuration> will be read as the + /// configuration. If this file is also a .NET .config file then you must specify + /// a configuration section for the log4net element otherwise .NET will + /// complain. Set the type for the section handler to , for example: + /// + /// + ///
+ /// + /// + /// + /// + /// The following example configures log4net using a configuration file, of which the + /// location is stored in the application's configuration file : + /// + /// + /// using log4net.Config; + /// using System.IO; + /// using System.Configuration; + /// + /// ... + /// + /// XmlConfigurator.Configure(new FileInfo(ConfigurationSettings.AppSettings["log4net-config-file"])); + /// + /// + /// In the .config file, the path to the log4net can be specified like this : + /// + /// + /// + /// + /// + /// + /// + /// + /// #else /// /// Configures the using the specified configuration @@ -500,20 +500,20 @@ static public ICollection Configure(ILoggerRepository repository, XmlElement ele /// /// #endif - static public ICollection Configure(ILoggerRepository repository, FileInfo configFile) - { - ArrayList configurationMessages = new ArrayList(); + static public ICollection Configure(ILoggerRepository repository, FileInfo configFile) + { + ArrayList configurationMessages = new ArrayList(); - using (new LogLog.LogReceivedAdapter(configurationMessages)) - { - InternalConfigure(repository, configFile); - } + using (new LogLog.LogReceivedAdapter(configurationMessages)) + { + InternalConfigure(repository, configFile); + } - repository.ConfigurationMessages = configurationMessages; + repository.ConfigurationMessages = configurationMessages; + + return configurationMessages; + } - return configurationMessages; - } - static private void InternalConfigure(ILoggerRepository repository, FileInfo configFile) { LogLog.Debug(declaringType, "configuring repository [" + repository.Name + "] using file [" + configFile + "]"); @@ -530,7 +530,7 @@ static private void InternalConfigure(ILoggerRepository repository, FileInfo con { // Open the file for reading FileStream fs = null; - + // Try hard to open the file for(int retry = 5; --retry >= 0; ) { @@ -573,36 +573,36 @@ static private void InternalConfigure(ILoggerRepository repository, FileInfo con } } - /// - /// Configures the using the specified configuration - /// URI. - /// - /// The repository to configure. - /// A URI to load the XML configuration from. - /// - /// - /// The configuration data must be valid XML. It must contain - /// at least one element called log4net that holds - /// the configuration data. - /// - /// - /// The must support the URI scheme specified. - /// - /// - static public ICollection Configure(ILoggerRepository repository, Uri configUri) - { - ArrayList configurationMessages = new ArrayList(); - - using (new LogLog.LogReceivedAdapter(configurationMessages)) - { - InternalConfigure(repository, configUri); - } - - repository.ConfigurationMessages = configurationMessages; - - return configurationMessages; - } - + /// + /// Configures the using the specified configuration + /// URI. + /// + /// The repository to configure. + /// A URI to load the XML configuration from. + /// + /// + /// The configuration data must be valid XML. It must contain + /// at least one element called log4net that holds + /// the configuration data. + /// + /// + /// The must support the URI scheme specified. + /// + /// + static public ICollection Configure(ILoggerRepository repository, Uri configUri) + { + ArrayList configurationMessages = new ArrayList(); + + using (new LogLog.LogReceivedAdapter(configurationMessages)) + { + InternalConfigure(repository, configUri); + } + + repository.ConfigurationMessages = configurationMessages; + + return configurationMessages; + } + static private void InternalConfigure(ILoggerRepository repository, Uri configUri) { LogLog.Debug(declaringType, "configuring repository [" + repository.Name + "] using URI ["+configUri+"]"); @@ -667,36 +667,36 @@ static private void InternalConfigure(ILoggerRepository repository, Uri configUr } } - /// - /// Configures the using the specified configuration - /// file. - /// - /// The repository to configure. - /// The stream to load the XML configuration from. - /// - /// - /// The configuration data must be valid XML. It must contain - /// at least one element called log4net that holds - /// the configuration data. - /// - /// - /// Note that this method will NOT close the stream parameter. - /// - /// - static public ICollection Configure(ILoggerRepository repository, Stream configStream) - { - ArrayList configurationMessages = new ArrayList(); - - using (new LogLog.LogReceivedAdapter(configurationMessages)) - { - InternalConfigure(repository, configStream); - } - - repository.ConfigurationMessages = configurationMessages; - - return configurationMessages; - } - + /// + /// Configures the using the specified configuration + /// file. + /// + /// The repository to configure. + /// The stream to load the XML configuration from. + /// + /// + /// The configuration data must be valid XML. It must contain + /// at least one element called log4net that holds + /// the configuration data. + /// + /// + /// Note that this method will NOT close the stream parameter. + /// + /// + static public ICollection Configure(ILoggerRepository repository, Stream configStream) + { + ArrayList configurationMessages = new ArrayList(); + + using (new LogLog.LogReceivedAdapter(configurationMessages)) + { + InternalConfigure(repository, configStream); + } + + repository.ConfigurationMessages = configurationMessages; + + return configurationMessages; + } + static private void InternalConfigure(ILoggerRepository repository, Stream configStream) { LogLog.Debug(declaringType, "configuring repository [" + repository.Name + "] using stream"); @@ -708,7 +708,11 @@ static private void InternalConfigure(ILoggerRepository repository, Stream confi else { // Load the config file into a document +#if NETSTANDARD1_3 XmlDocument doc = new XmlDocument(); +#else + XmlDocument doc = new XmlDocument { XmlResolver = null }; +#endif try { #if (NETCF) @@ -717,8 +721,8 @@ static private void InternalConfigure(ILoggerRepository repository, Stream confi #elif NET_2_0 || NETSTANDARD // Allow the DTD to specify entity includes XmlReaderSettings settings = new XmlReaderSettings(); - // .NET 4.0 warning CS0618: 'System.Xml.XmlReaderSettings.ProhibitDtd' - // is obsolete: 'Use XmlReaderSettings.DtdProcessing property instead.' + // .NET 4.0 warning CS0618: 'System.Xml.XmlReaderSettings.ProhibitDtd' + // is obsolete: 'Use XmlReaderSettings.DtdProcessing property instead.' #if NETSTANDARD1_3 // TODO DtdProcessing.Parse not yet available (https://github.com/dotnet/corefx/issues/4376) settings.DtdProcessing = DtdProcessing.Ignore; #elif !NET_4_0 && !MONO_4_0 && !NETSTANDARD2_0 @@ -728,17 +732,17 @@ static private void InternalConfigure(ILoggerRepository repository, Stream confi #endif // Create a reader over the input stream - XmlReader xmlReader = XmlReader.Create(configStream, settings); + using XmlReader xmlReader = XmlReader.Create(configStream, settings); #else // Create a validating reader around a text reader for the file stream - XmlValidatingReader xmlReader = new XmlValidatingReader(new XmlTextReader(configStream)); + using XmlValidatingReader xmlReader = new XmlValidatingReader(new XmlTextReader(configStream)); // Specify that the reader should not perform validation, but that it should // expand entity refs. xmlReader.ValidationType = ValidationType.None; xmlReader.EntityHandling = EntityHandling.ExpandEntities; #endif - + // load the data into the document doc.Load(xmlReader); } @@ -801,58 +805,58 @@ static private void InternalConfigure(ILoggerRepository repository, Stream confi /// static public ICollection ConfigureAndWatch(FileInfo configFile) { - ArrayList configurationMessages = new ArrayList(); + ArrayList configurationMessages = new ArrayList(); - ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly()); + ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly()); - using (new LogLog.LogReceivedAdapter(configurationMessages)) - { - InternalConfigureAndWatch(repository, configFile); - } + using (new LogLog.LogReceivedAdapter(configurationMessages)) + { + InternalConfigureAndWatch(repository, configFile); + } - repository.ConfigurationMessages = configurationMessages; + repository.ConfigurationMessages = configurationMessages; - return configurationMessages; + return configurationMessages; } #endif // !NETSTANDARD1_3 - /// - /// Configures the using the file specified, - /// monitors the file for changes and reloads the configuration if a change - /// is detected. - /// - /// The repository to configure. - /// The XML file to load the configuration from. - /// - /// - /// The configuration file must be valid XML. It must contain - /// at least one element called log4net that holds - /// the configuration data. - /// - /// - /// The configuration file will be monitored using a - /// and depends on the behavior of that class. - /// - /// - /// For more information on how to configure log4net using - /// a separate configuration file, see . - /// - /// - /// - static public ICollection ConfigureAndWatch(ILoggerRepository repository, FileInfo configFile) - { - ArrayList configurationMessages = new ArrayList(); - - using (new LogLog.LogReceivedAdapter(configurationMessages)) - { - InternalConfigureAndWatch(repository, configFile); - } - - repository.ConfigurationMessages = configurationMessages; - - return configurationMessages; - } - + /// + /// Configures the using the file specified, + /// monitors the file for changes and reloads the configuration if a change + /// is detected. + /// + /// The repository to configure. + /// The XML file to load the configuration from. + /// + /// + /// The configuration file must be valid XML. It must contain + /// at least one element called log4net that holds + /// the configuration data. + /// + /// + /// The configuration file will be monitored using a + /// and depends on the behavior of that class. + /// + /// + /// For more information on how to configure log4net using + /// a separate configuration file, see . + /// + /// + /// + static public ICollection ConfigureAndWatch(ILoggerRepository repository, FileInfo configFile) + { + ArrayList configurationMessages = new ArrayList(); + + using (new LogLog.LogReceivedAdapter(configurationMessages)) + { + InternalConfigureAndWatch(repository, configFile); + } + + repository.ConfigurationMessages = configurationMessages; + + return configurationMessages; + } + static private void InternalConfigureAndWatch(ILoggerRepository repository, FileInfo configFile) { LogLog.Debug(declaringType, "configuring repository [" + repository.Name + "] using file [" + configFile + "] watching for file updates"); @@ -868,23 +872,23 @@ static private void InternalConfigureAndWatch(ILoggerRepository repository, File try { - lock (m_repositoryName2ConfigAndWatchHandler) - { - // support multiple repositories each having their own watcher - ConfigureAndWatchHandler handler = + lock (m_repositoryName2ConfigAndWatchHandler) + { + // support multiple repositories each having their own watcher + ConfigureAndWatchHandler handler = (ConfigureAndWatchHandler)m_repositoryName2ConfigAndWatchHandler[configFile.FullName]; - if (handler != null) - { + if (handler != null) + { m_repositoryName2ConfigAndWatchHandler.Remove(configFile.FullName); - handler.Dispose(); - } + handler.Dispose(); + } - // Create and start a watch handler that will reload the - // configuration whenever the config file is modified. - handler = new ConfigureAndWatchHandler(repository, configFile); + // Create and start a watch handler that will reload the + // configuration whenever the config file is modified. + handler = new ConfigureAndWatchHandler(repository, configFile); m_repositoryName2ConfigAndWatchHandler[configFile.FullName] = handler; - } + } } catch(Exception ex) { @@ -938,15 +942,15 @@ private sealed class ConfigureAndWatchHandler : IDisposable /// private const int TimeoutMillis = 500; - /// - /// Watches file for changes. This object should be disposed when no longer - /// needed to free system handles on the watched resources. - /// - private FileSystemWatcher m_watcher; + /// + /// Watches file for changes. This object should be disposed when no longer + /// needed to free system handles on the watched resources. + /// + private FileSystemWatcher m_watcher; /// /// Initializes a new instance of the class to - /// watch a specified config file used to configure a repository. + /// watch a specified config file used to configure a repository. /// /// The repository to configure. /// The configuration file to watch. @@ -956,9 +960,9 @@ private sealed class ConfigureAndWatchHandler : IDisposable /// /// #if NET_4_0 || MONO_4_0 || NETSTANDARD - [System.Security.SecuritySafeCritical] + [System.Security.SecuritySafeCritical] #endif - public ConfigureAndWatchHandler(ILoggerRepository repository, FileInfo configFile) + public ConfigureAndWatchHandler(ILoggerRepository repository, FileInfo configFile) { m_repository = repository; m_configFile = configFile; @@ -982,7 +986,7 @@ public ConfigureAndWatchHandler(ILoggerRepository repository, FileInfo configFil m_watcher.EnableRaisingEvents = true; // Create the timer that will be used to deliver events. Set as disabled - m_timer = new Timer(new TimerCallback(OnWatchedFileChange), null, Timeout.Infinite, Timeout.Infinite); + m_timer = new Timer(new TimerCallback(OnWatchedFileChange), null, Timeout.Infinite, Timeout.Infinite); } /// @@ -1032,18 +1036,18 @@ private void OnWatchedFileChange(object state) XmlConfigurator.InternalConfigure(m_repository, m_configFile); } - /// - /// Release the handles held by the watcher and timer. - /// + /// + /// Release the handles held by the watcher and timer. + /// #if NET_4_0 || MONO_4_0 || NETSTANDARD - [System.Security.SecuritySafeCritical] + [System.Security.SecuritySafeCritical] #endif - public void Dispose() - { - m_watcher.EnableRaisingEvents = false; - m_watcher.Dispose(); - m_timer.Dispose(); - } + public void Dispose() + { + m_watcher.EnableRaisingEvents = false; + m_watcher.Dispose(); + m_timer.Dispose(); + } } #endif @@ -1066,7 +1070,7 @@ public void Dispose() /// to load the configuration from an . /// /// - static private void InternalConfigureFromXml(ILoggerRepository repository, XmlElement element) + static private void InternalConfigureFromXml(ILoggerRepository repository, XmlElement element) { if (element == null) { @@ -1090,36 +1094,40 @@ static private void InternalConfigureFromXml(ILoggerRepository repository, XmlEl // Copy the xml data into the root of a new document // this isolates the xml config data from the rest of // the document +#if NETSTANDARD1_3 XmlDocument newDoc = new XmlDocument(); +#else + XmlDocument newDoc = new XmlDocument { XmlResolver = null }; +#endif XmlElement newElement = (XmlElement)newDoc.AppendChild(newDoc.ImportNode(element, true)); // Pass the configurator the config element configurableRepository.Configure(newElement); - } + } } } #endregion Private Static Methods - #region Private Static Fields - - /// - /// Maps repository names to ConfigAndWatchHandler instances to allow a particular - /// ConfigAndWatchHandler to dispose of its FileSystemWatcher when a repository is - /// reconfigured. - /// - private readonly static Hashtable m_repositoryName2ConfigAndWatchHandler = new Hashtable(); - - /// - /// The fully qualified type of the XmlConfigurator class. - /// - /// - /// Used by the internal logger to record the Type of the - /// log message. - /// - private readonly static Type declaringType = typeof(XmlConfigurator); - - #endregion Private Static Fields + #region Private Static Fields + + /// + /// Maps repository names to ConfigAndWatchHandler instances to allow a particular + /// ConfigAndWatchHandler to dispose of its FileSystemWatcher when a repository is + /// reconfigured. + /// + private readonly static Hashtable m_repositoryName2ConfigAndWatchHandler = new Hashtable(); + + /// + /// The fully qualified type of the XmlConfigurator class. + /// + /// + /// Used by the internal logger to record the Type of the + /// log message. + /// + private readonly static Type declaringType = typeof(XmlConfigurator); + + #endregion Private Static Fields } } diff --git a/src/log4net/Layout/Layout2RawLayoutAdapter.cs b/src/log4net/Layout/Layout2RawLayoutAdapter.cs index 5b3707c2..49457841 100644 --- a/src/log4net/Layout/Layout2RawLayoutAdapter.cs +++ b/src/log4net/Layout/Layout2RawLayoutAdapter.cs @@ -83,7 +83,7 @@ public Layout2RawLayoutAdapter(ILayout layout) /// virtual public object Format(LoggingEvent loggingEvent) { - StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); + using StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); m_layout.Format(writer, loggingEvent); return writer.ToString(); } diff --git a/src/log4net/Layout/LayoutSkeleton.cs b/src/log4net/Layout/LayoutSkeleton.cs index b373ee14..afae60e7 100644 --- a/src/log4net/Layout/LayoutSkeleton.cs +++ b/src/log4net/Layout/LayoutSkeleton.cs @@ -117,9 +117,9 @@ protected LayoutSkeleton() /// If any of the configuration properties are modified then /// must be called again. /// - /// - /// This method must be implemented by the subclass. - /// + /// + /// This method must be implemented by the subclass. + /// /// abstract public void ActivateOptions(); @@ -140,93 +140,93 @@ protected LayoutSkeleton() /// abstract public void Format(TextWriter writer, LoggingEvent loggingEvent); - /// - /// Convenience method for easily formatting the logging event into a string variable. - /// - /// - /// - /// Creates a new StringWriter instance to store the formatted logging event. - /// - public string Format(LoggingEvent loggingEvent) - { - StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); - Format(writer, loggingEvent); - return writer.ToString(); - } - - /// - /// The content type output by this layout. - /// - /// The content type is "text/plain" - /// - /// - /// The content type output by this layout. - /// - /// - /// This base class uses the value "text/plain". - /// To change this value a subclass must override this - /// property. - /// - /// - virtual public string ContentType - { - get { return "text/plain"; } - } - - /// - /// The header for the layout format. - /// - /// the layout header - /// - /// - /// The Header text will be appended before any logging events - /// are formatted and appended. - /// - /// - virtual public string Header - { - get { return m_header; } - set { m_header = value; } - } - - /// - /// The footer for the layout format. - /// - /// the layout footer - /// - /// - /// The Footer text will be appended after all the logging events - /// have been formatted and appended. - /// - /// - virtual public string Footer - { - get { return m_footer; } - set { m_footer = value; } - } - - /// - /// Flag indicating if this layout handles exceptions - /// - /// false if this layout handles exceptions - /// - /// - /// If this layout handles the exception object contained within - /// , then the layout should return - /// false. Otherwise, if the layout ignores the exception - /// object, then the layout should return true. - /// - /// - /// Set this value to override a this default setting. The default - /// value is true, this layout does not handle the exception. - /// - /// - virtual public bool IgnoresException - { - get { return m_ignoresException; } - set { m_ignoresException = value; } - } - - #endregion + /// + /// Convenience method for easily formatting the logging event into a string variable. + /// + /// + /// + /// Creates a new StringWriter instance to store the formatted logging event. + /// + public string Format(LoggingEvent loggingEvent) + { + using StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); + Format(writer, loggingEvent); + return writer.ToString(); + } + + /// + /// The content type output by this layout. + /// + /// The content type is "text/plain" + /// + /// + /// The content type output by this layout. + /// + /// + /// This base class uses the value "text/plain". + /// To change this value a subclass must override this + /// property. + /// + /// + virtual public string ContentType + { + get { return "text/plain"; } + } + + /// + /// The header for the layout format. + /// + /// the layout header + /// + /// + /// The Header text will be appended before any logging events + /// are formatted and appended. + /// + /// + virtual public string Header + { + get { return m_header; } + set { m_header = value; } + } + + /// + /// The footer for the layout format. + /// + /// the layout footer + /// + /// + /// The Footer text will be appended after all the logging events + /// have been formatted and appended. + /// + /// + virtual public string Footer + { + get { return m_footer; } + set { m_footer = value; } + } + + /// + /// Flag indicating if this layout handles exceptions + /// + /// false if this layout handles exceptions + /// + /// + /// If this layout handles the exception object contained within + /// , then the layout should return + /// false. Otherwise, if the layout ignores the exception + /// object, then the layout should return true. + /// + /// + /// Set this value to override a this default setting. The default + /// value is true, this layout does not handle the exception. + /// + /// + virtual public bool IgnoresException + { + get { return m_ignoresException; } + set { m_ignoresException = value; } + } + + #endregion } } diff --git a/src/log4net/ObjectRenderer/RendererMap.cs b/src/log4net/ObjectRenderer/RendererMap.cs index 7f4db96c..666dfda4 100644 --- a/src/log4net/ObjectRenderer/RendererMap.cs +++ b/src/log4net/ObjectRenderer/RendererMap.cs @@ -45,7 +45,7 @@ namespace log4net.ObjectRenderer /// Gert Driesen public class RendererMap { - private readonly static Type declaringType = typeof(RendererMap); + private readonly static Type declaringType = typeof(RendererMap); #region Member Variables @@ -94,7 +94,7 @@ public string FindAndRender(object obj) return strData; } - StringWriter stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); + using StringWriter stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); FindAndRender(obj, stringWriter); return stringWriter.ToString(); } diff --git a/src/log4net/Util/PatternString.cs b/src/log4net/Util/PatternString.cs index 4a840eda..72d9ba92 100644 --- a/src/log4net/Util/PatternString.cs +++ b/src/log4net/Util/PatternString.cs @@ -467,7 +467,7 @@ public void Format(TextWriter writer) /// public string Format() { - StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); + using StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); Format(writer); return writer.ToString(); }