[
https://issues.apache.org/jira/browse/LOG4NET-88?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12702032#action_12702032
]
Lewis Moten commented on LOG4NET-88:
------------------------------------
Ron, you need to activate the options "after" you set the connection string.
Otherwise an error occurs (The ConnectionString property has not been
initialized.) Here is a more robust version of the code.
/// <summary>
/// An appender for Log4Net that uses a database based on the connection
string name.
/// </summary>
public class Log4NetConnectionStringNameAdoNetAppender : AdoNetAppender
{
private static ILog _Log;
/// <summary>
/// Gets the log.
/// </summary>
/// <value>The log.</value>
protected static ILog Log
{
get
{
if (_Log == null)
_Log =
LogManager.GetLogger(typeof(Log4NetConnectionStringNameAdoNetAppender));
return _Log;
}
}
private string _ConnectionStringName;
/// <summary>
/// Initialize the appender based on the options set
/// </summary>
/// <remarks>
/// <para>
/// This is part of the <see cref="T:log4net.Core.IOptionHandler"/>
delayed object
/// activation scheme. The <see
cref="M:log4net.Appender.AdoNetAppender.ActivateOptions"/> method must
/// be called on this object after the configuration properties have
/// been set. Until <see
cref="M:log4net.Appender.AdoNetAppender.ActivateOptions"/> is called this
/// object is in an undefined state and must not be used.
/// </para>
/// <para>
/// If any of the configuration properties are modified then
/// <see cref="M:log4net.Appender.AdoNetAppender.ActivateOptions"/>
must be called again.
/// </para>
/// </remarks>
public override void ActivateOptions()
{
PopulateConnectionString();
base.ActivateOptions();
}
/// <summary>
/// Populates the connection string.
/// </summary>
private void PopulateConnectionString()
{
// if connection string already defined, do nothing
if (!String.IsNullOrEmpty(ConnectionString)) return;
// if connection string name is not available, do nothing
if (String.IsNullOrEmpty(ConnectionStringName)) return;
// grab connection string settings
ConnectionStringSettings settings = ConfigurationManager
.ConnectionStrings[ConnectionStringName];
// if connection string name was not found in settings
if (settings == null)
{
// log error
if (Log.IsErrorEnabled)
Log.ErrorFormat("Connection String Name not found in
Configuration: {0}",
ConnectionStringName);
// do nothing more
return;
}
// retrieve connection string from the name
ConnectionString = settings.ConnectionString;
}
/// <summary>
/// Gets or sets the name of the connection string.
/// </summary>
/// <value>The name of the connection string.</value>
public string ConnectionStringName
{
get { return _ConnectionStringName; }
set { _ConnectionStringName = value; }
}
}
> [PATCH] to AdoNetAppender.cs to support .NET 2.0 connectionStrings
> configuration section
> ----------------------------------------------------------------------------------------
>
> Key: LOG4NET-88
> URL: https://issues.apache.org/jira/browse/LOG4NET-88
> Project: Log4net
> Issue Type: Improvement
> Components: Appenders
> Affects Versions: 1.2.10
> Environment: Windows XP .NET 2.0
> Reporter: Haacked
> Assignee: Ron Grabowski
> Fix For: 1.2.11
>
> Attachments: AdoNetAppenderConnectionStringName.patch
>
>
> This is a patch for [LOG4NET-78]
> https://issues.apache.org/jira/browse/LOG4NET-78. This adds a new
> "connectionStringName" attribute (and corresponding ConnectionStringName)
> property to the AdoNetAppender class. This is a reference to a connection
> string within the <ConnectionStrings> section of an App.config or Web.config
> file.
> The choice of name "connectionStringName" is consistent with the naming that
> Microsoft uses in the various Providers such as SqlMembershipProvider.
> I made sure to put this code in between #if NET_2_0 blocks. However, the
> patch does not include the required change to the log4net.csproj file which
> needs to reference the System.Configuration.dll
> (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.configuration.dll)
> assembly.
> This is a .NET 2.0 only assembly so I was unsure how to add a conditional
> compilation element so that the project reference would not break log4net for
> .NET 1.0 and .NET 1.1.
> If there is a better way to do so, please let me know!
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.