nicko 2005/02/07 14:33:35
Modified: src/Appender EventLogAppender.cs RollingFileAppender.cs
src/Layout/Pattern NamedPatternConverter.cs
src/Util OptionConverter.cs SystemInfo.cs
src/Util/PatternStringConverters
RandomStringPatternConverter.cs
Log:
Added SystemInfo.TryParse methods for parsing strings to integers. These
methods give a performance boost when the string cannot be parsed.
Revision Changes Path
1.15 +6 -5 logging-log4net/src/Appender/EventLogAppender.cs
Index: EventLogAppender.cs
===================================================================
RCS file: /home/cvs/logging-log4net/src/Appender/EventLogAppender.cs,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- EventLogAppender.cs 17 Jan 2005 20:18:41 -0000 1.14
+++ EventLogAppender.cs 7 Feb 2005 22:33:34 -0000 1.15
@@ -331,16 +331,17 @@
else
{
string eventIDPropertyString =
eventIDPropertyObj as string;
- if (eventIDPropertyString != null)
+ if (eventIDPropertyString != null &&
eventIDPropertyString.Length > 0)
{
// Read the string property
into a number
- try
+ int intVal;
+ if
(SystemInfo.TryParse(eventIDPropertyString, out intVal))
{
- eventID =
int.Parse(eventIDPropertyString, CultureInfo.InvariantCulture);
+ eventID = intVal;
}
- catch(Exception ex)
+ else
{
-
ErrorHandler.Error("Unable to parse event ID property [" +
eventIDPropertyString + "].", ex);
+
ErrorHandler.Error("Unable to parse event ID property [" +
eventIDPropertyString + "].");
}
}
}
1.19 +23 -19 logging-log4net/src/Appender/RollingFileAppender.cs
Index: RollingFileAppender.cs
===================================================================
RCS file: /home/cvs/logging-log4net/src/Appender/RollingFileAppender.cs,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- RollingFileAppender.cs 4 Feb 2005 02:54:50 -0000 1.18
+++ RollingFileAppender.cs 7 Feb 2005 22:33:34 -0000 1.19
@@ -800,36 +800,40 @@
try
{
// Bump the counter up to the highest count
seen so far
- int backup =
int.Parse(curFileName.Substring(index + 1),
System.Globalization.NumberFormatInfo.InvariantInfo);
- if (backup > m_curSizeRollBackups)
+// int backup =
int.Parse(curFileName.Substring(index + 1),
System.Globalization.NumberFormatInfo.InvariantInfo);
+ int backup;
+ if
(SystemInfo.TryParse(curFileName.Substring(index + 1), out backup))
{
- if (0 == m_maxSizeRollBackups)
+ if (backup > m_curSizeRollBackups)
{
- // Stay at zero when zero
backups are desired
- }
- else if (-1 == m_maxSizeRollBackups)
- {
- // Infinite backups, so go as
high as the highest value
- m_curSizeRollBackups = backup;
- }
- else
- {
- // Backups limited to a finite
number
- if (m_countDirection >= 0)
+ if (0 == m_maxSizeRollBackups)
{
- // Go with the highest
file when counting up
+ // Stay at zero when
zero backups are desired
+ }
+ else if (-1 ==
m_maxSizeRollBackups)
+ {
+ // Infinite backups, so
go as high as the highest value
m_curSizeRollBackups =
backup;
- }
+ }
else
{
- // Clip to the limit
when counting down
- if (backup <=
m_maxSizeRollBackups)
+ // Backups limited to a
finite number
+ if (m_countDirection >=
0)
{
+ // Go with the
highest file when counting up
m_curSizeRollBackups = backup;
+ }
+ else
+ {
+ // Clip to the
limit when counting down
+ if (backup <=
m_maxSizeRollBackups)
+ {
+
m_curSizeRollBackups = backup;
+ }
}
}
+
LogLog.Debug("RollingFileAppender: File name ["+curFileName+"] moves current
count to ["+m_curSizeRollBackups+"]");
}
- LogLog.Debug("RollingFileAppender: File
name ["+curFileName+"] moves current count to ["+m_curSizeRollBackups+"]");
}
}
catch(FormatException)
1.7 +13 -17
logging-log4net/src/Layout/Pattern/NamedPatternConverter.cs
Index: NamedPatternConverter.cs
===================================================================
RCS file:
/home/cvs/logging-log4net/src/Layout/Pattern/NamedPatternConverter.cs,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- NamedPatternConverter.cs 17 Jan 2005 20:18:45 -0000 1.6
+++ NamedPatternConverter.cs 7 Feb 2005 22:33:35 -0000 1.7
@@ -65,32 +65,28 @@
/// </remarks>
public void ActivateOptions()
{
- if (Option == null)
- {
- m_precision = 0;
- }
- else
+ m_precision = 0;
+
+ if (Option != null)
{
string optStr = Option.Trim();
- if (optStr.Length == 0)
+ if (optStr.Length > 0)
{
- m_precision = 0;
- }
- else
- {
- try
+ int precisionVal;
+ if (SystemInfo.TryParse(optStr, out
precisionVal))
{
- m_precision = int.Parse(optStr,
NumberFormatInfo.InvariantInfo);
- if (m_precision <= 0)
+ if (precisionVal <= 0)
{
LogLog.Error("NamedPatternConverter: Precision option (" + optStr + ") isn't a
positive integer.");
- m_precision = 0;
+ }
+ else
+ {
+ m_precision =
precisionVal;
}
}
- catch (Exception e)
+ else
{
-
LogLog.Error("NamedPatternConverter: Precision option \"" + optStr + "\" not a
decimal integer.", e);
- m_precision = 0;
+
LogLog.Error("NamedPatternConverter: Precision option \"" + optStr + "\" not a
decimal integer.");
}
}
}
1.13 +6 -5 logging-log4net/src/Util/OptionConverter.cs
Index: OptionConverter.cs
===================================================================
RCS file: /home/cvs/logging-log4net/src/Util/OptionConverter.cs,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- OptionConverter.cs 4 Feb 2005 03:05:01 -0000 1.12
+++ OptionConverter.cs 7 Feb 2005 22:33:35 -0000 1.13
@@ -234,14 +234,15 @@
{
// Try again to remove whitespace between the
number and the size specifier
s = s.Trim();
-
- try
+
+ long longVal;
+ if (SystemInfo.TryParse(s, out longVal))
{
- return long.Parse(s,
NumberStyles.Integer, NumberFormatInfo.InvariantInfo) * multiplier;
+ return longVal * multiplier;
}
- catch (Exception e)
+ else
{
- LogLog.Error("OptionConverter: [" + s +
"] is not in the proper file size form.", e);
+ LogLog.Error("OptionConverter: ["+ s
+"] is not in the correct file size syntax.");
}
}
return defaultValue;
1.10 +69 -0 logging-log4net/src/Util/SystemInfo.cs
Index: SystemInfo.cs
===================================================================
RCS file: /home/cvs/logging-log4net/src/Util/SystemInfo.cs,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- SystemInfo.cs 7 Feb 2005 04:05:50 -0000 1.9
+++ SystemInfo.cs 7 Feb 2005 22:33:35 -0000 1.10
@@ -652,6 +652,75 @@
#endif
}
+
+ /// <summary>
+ /// Parse a string into an <see cref="Int32"/> value
+ /// </summary>
+ /// <param name="s">the string to parse</param>
+ /// <param name="val">out param where the parsed value is
placed</param>
+ /// <returns><c>true</c> if the string was able to be parsed
into an integer</returns>
+ /// <remarks>
+ /// <para>
+ /// Attempts to parse the string into an integer. If the string
cannot
+ /// be parsed then this method returns <c>false</c>. The method
does not throw an exception.
+ /// </para>
+ /// </remarks>
+ public static bool TryParse(string s, out int val)
+ {
+ // Initialise out param
+ val = 0;
+
+ try
+ {
+ double doubleVal;
+ if (Double.TryParse(s,
System.Globalization.NumberStyles.Integer,
System.Globalization.CultureInfo.InvariantCulture, out doubleVal))
+ {
+ val = Convert.ToInt32(doubleVal);
+ return true;
+ }
+ }
+ catch
+ {
+ // Ignore exception, just return false
+ }
+
+ return false;
+ }
+
+ /// <summary>
+ /// Parse a string into an <see cref="Int64"/> value
+ /// </summary>
+ /// <param name="s">the string to parse</param>
+ /// <param name="val">out param where the parsed value is
placed</param>
+ /// <returns><c>true</c> if the string was able to be parsed
into an integer</returns>
+ /// <remarks>
+ /// <para>
+ /// Attempts to parse the string into an integer. If the string
cannot
+ /// be parsed then this method returns <c>false</c>. The method
does not throw an exception.
+ /// </para>
+ /// </remarks>
+ public static bool TryParse(string s, out long val)
+ {
+ // Initialise out param
+ val = 0;
+
+ try
+ {
+ double doubleVal;
+ if (Double.TryParse(s,
System.Globalization.NumberStyles.Integer,
System.Globalization.CultureInfo.InvariantCulture, out doubleVal))
+ {
+ val = Convert.ToInt64(doubleVal);
+ return true;
+ }
+ }
+ catch
+ {
+ // Ignore exception, just return false
+ }
+
+ return false;
+ }
+
#endregion Public Static Methods
#region Private Static Methods
1.5 +5 -4
logging-log4net/src/Util/PatternStringConverters/RandomStringPatternConverter.cs
Index: RandomStringPatternConverter.cs
===================================================================
RCS file:
/home/cvs/logging-log4net/src/Util/PatternStringConverters/RandomStringPatternConverter.cs,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- RandomStringPatternConverter.cs 17 Jan 2005 20:18:50 -0000 1.4
+++ RandomStringPatternConverter.cs 7 Feb 2005 22:33:35 -0000 1.5
@@ -79,13 +79,14 @@
string optionStr = Option;
if (optionStr != null && optionStr.Length > 0)
{
- try
+ int lengthVal;
+ if (SystemInfo.TryParse(optionStr, out
lengthVal))
{
- m_length = int.Parse(optionStr,
System.Globalization.CultureInfo.InvariantCulture);
+ m_length = lengthVal;
}
- catch (Exception e)
+ else
{
-
LogLog.Error("RandomStringPatternConverter: Could not convert Option
["+optionStr+"] to Length Int32", e);
+
LogLog.Error("RandomStringPatternConverter: Could not convert Option
["+optionStr+"] to Length Int32");
}
}
}