You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by mmingfeilam <la...@comcast.net> on 2012/02/01 20:12:12 UTC

log4net AdoNetAppender common connectionstring value

i have two exes in my solution that are run simultaeneously.  each exe has a
log4net.xml file with a AdoNetAppender in it:
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="100" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data,
Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="data source=[database server];initial
catalog=[database name];integrated security=false;persist security
info=True;User ID=[user];Password=[password]" />
    <commandText value="INSERT INTO Log
([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date,
@thread, @log_level, @logger, @message, @exception)" />
    <parameter>

is there any way to move the value of the connectionstring to a common file
and just reference it by name?  would it be possible to move the appenders
to one common location as well?  i want to do it using a config file and not
programmatically.  thanks.


-- 
View this message in context: http://old.nabble.com/log4net-AdoNetAppender-common-connectionstring-value-tp33244792p33244792.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


Re: log4net AdoNetAppender common connectionstring value

Posted by Ron Grabowski <ro...@yahoo.com>.
Maybe you could share a common App.Config between the two apps and use this to set the connection string:

http://logging.apache.org/log4net/release/sdk/log4net.Appender.AdoNetAppender.ConnectionStringName.html



________________________________
 From: mmingfeilam <la...@comcast.net>
To: log4net-user@logging.apache.org 
Sent: Wednesday, February 1, 2012 7:16 PM
Subject: Re: log4net AdoNetAppender common connectionstring value
 


"
I do something along that line. I have a connection string inside
machine.config and I wrapped l4n in a wrapper dll that's used in many
projects. I actually deploy a logging config file alongside the wrapper, so
some of the code here is accessing settings inside that. The logging CN is
in machine.config because it's used elsewhere, as well.
 
So the wraper calls ConfigureLogging() from wrapper logging methods that I
expose. My apps don't actually deal with l4n directly. Each one simply
checks if initialization is needed before forwarding into l4n."
 
so you are doing this programmatically, is there a way to do it using the
configuration file, something along the line of:

<connectionString configSource="myConnectionStringFile" .../>
-- 
View this message in context: http://old.nabble.com/log4net-AdoNetAppender-common-connectionstring-value-tp33244792p33245873.html
Sent from the Log4net - Users mailing list archive at Nabble.com.

Re: log4net AdoNetAppender common connectionstring value

Posted by mmingfeilam <la...@comcast.net>.

"
I do something along that line. I have a connection string inside
machine.config and I wrapped l4n in a wrapper dll that's used in many
projects. I actually deploy a logging config file alongside the wrapper, so
some of the code here is accessing settings inside that. The logging CN is
in machine.config because it's used elsewhere, as well.
 
So the wraper calls ConfigureLogging() from wrapper logging methods that I
expose. My apps don't actually deal with l4n directly. Each one simply
checks if initialization is needed before forwarding into l4n."
 
so you are doing this programmatically, is there a way to do it using the
configuration file, something along the line of:

<connectionString configSource="myConnectionStringFile" .../>
-- 
View this message in context: http://old.nabble.com/log4net-AdoNetAppender-common-connectionstring-value-tp33244792p33245873.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


Re: log4net AdoNetAppender common connectionstring value

Posted by Todd <to...@yahoo.com>.
I do something along that line. I have a connection string inside machine.config and I wrapped l4n in a wrapper dll that's used in many projects. I actually deploy a logging config file alongside the wrapper, so some of the code here is accessing settings inside that. The logging CN is in machine.config because it's used elsewhere, as well.
 
So the wraper calls ConfigureLogging() from wrapper logging methods that I expose. My apps don't actually deal with l4n directly. Each one simply checks if initialization is needed before forwarding into l4n.
 
In the wrapper class I do this:
 
        private static void ConfigureLogging()
        {
            string assemblyPath = string.Empty;
            Configuration cfg = null;
            try
            {
                string connectionString = string.Empty;
                assemblyPath = Assembly.GetExecutingAssembly().Location;
                try
                {
                    cfg = ConfigurationManager.OpenExeConfiguration(assemblyPath);
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Failed to load configuration file");
                    throw new ApplicationException(msg, ex);
                }
                if (!cfg.HasFile)
                {
                    string msg = string.Format("Failed to load configuration file");
                    throw new ApplicationException(msg);
                }
                // Connection strings come from machine.config, even though we're explicitly going after the app.dll.config
                if((cfg.ConnectionStrings == null) || cfg.ConnectionStrings.ConnectionStrings == null || cfg.ConnectionStrings.ConnectionStrings.Count == 0)
                    throw new ApplicationException("Framework could not to locate connection string collection in machine.config");

                ConnectionStringSettings cstr = cfg.ConnectionStrings.ConnectionStrings["CentralLogging"];
                if (cstr == null)
                    throw new ApplicationException("Framework could not locate 'CentralLogging' connection string.");
                connectionString = cfg.ConnectionStrings.ConnectionStrings["CentralLogging"].ConnectionString;
                connectionString += string.Format(";Application Name={0}; ", DatabaseApplicationName);
                try
                {
                    // Now we're using the local app.dll.config file
                    FileInfo fi = new FileInfo(cfg.FilePath);
                    if (fi.Exists)
                    {
                        // array of LogLog objects. Silly, eh?
                        ArrayList configurationMessages = (ArrayList)log4net.Config.XmlConfigurator.Configure(fi);
                        if (configurationMessages.Count > 0)
                        {
                            foreach (log4net.Util.LogLog l in configurationMessages)
                            {
                                if (l.Exception != null)
                                    _configurationMessages.Add(l.Exception);
                                else
                                    _configurationMessages.Add(l.Message);
                            }
                        }
                    }
                    else
                    {
                        string msg = string.Format("Missing configuration file'{0}'", assemblyPath);
                        throw new ApplicationException(msg);
                    }
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Framework unable to configure log4net by pointing it to the cfg file.", ex);
                }
                try
                {
                    ConnectLog4NetAppenders(connectionString);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Framework unable to connect log4net appenders.", ex);
                }
            }
            catch (Exception ex)
            {
                Exception innerEx = ex;
                while(innerEx.InnerException != null)
                    innerEx = innerEx.InnerException;
                string cfgFilePath = "Unknown";
                if (cfg != null)
                    cfgFilePath = cfg.FilePath;
 
                string msg = string.Format("{0}.\nAssembly Location: {1}\ncfg.FilePath: {2}\n", ex.Message, assemblyPath, cfgFilePath);
                throw new ApplicationException(msg, innerEx);
            }
        }
        private static void ConnectLog4NetAppenders(string connectionString)
        {
            log4net.Repository.Hierarchy.Hierarchy hier = log4net.LogManager.GetRepository() as log4net.Repository.Hierarchy.Hierarchy;
            if (hier != null)
            {
                //string cnStr = "data source=do-sqld1;initial catalog=Admin;integrated security=true;persist security info=True";
                foreach (log4net.Appender.IAppender appender in hier.GetAppenders())
                {
                    if (appender.GetType() == typeof(log4net.Appender.AdoNetAppender) && !string.IsNullOrEmpty(connectionString))
                    {
                        log4net.Appender.AdoNetAppender sqlAppender = (log4net.Appender.AdoNetAppender)appender;
                        sqlAppender.ConnectionString = connectionString;
                        // In order to get these custom id values into the log, we have to dynamically create parameters and populate them.
                        // This is because log4net does not expose the command object, or existing parameter objects. If we defined them
                        // in the config file, we'd be updating their properties here, but instead, we have to create them entirely here.
                        // Note that the command text is still defined in the config file and references these parameters, even though they
                        // won't exist up until this point.
                        sqlAppender.AddParameter(CreateSystemIdParameter("@system_name", SystemName));
                        sqlAppender.AddParameter(CreateSystemIdParameter("@system_tag", SystemTag));
                        
                        sqlAppender.ActivateOptions(); //refresh settings of appender  
                    }
                }
            }
        }

  

________________________________
 From: mmingfeilam <la...@comcast.net>
To: log4net-user@logging.apache.org 
Sent: Wednesday, February 1, 2012 2:12 PM
Subject: log4net AdoNetAppender common connectionstring value
  

i have two exes in my solution that are run simultaeneously.  each exe has a
log4net.xml file with a AdoNetAppender in it:
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="100" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data,
Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="data source=[database server];initial
catalog=[database name];integrated security=false;persist security
info=True;User ID=[user];Password=[password]" />
    <commandText value="INSERT INTO Log
([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date,
@thread, @log_level, @logger, @message, @exception)" />
    <parameter>

is there any way to move the value of the connectionstring to a common file
and just reference it by name?  would it be possible to move the appenders
to one common location as well?  i want to do it using a config file and not
programmatically.  thanks.


-- 
View this message in context: http://old.nabble.com/log4net-AdoNetAppender-common-connectionstring-value-tp33244792p33244792.html
Sent from the Log4net - Users mailing list archive at Nabble.com.