Share via


Need to setup a entiy connection string to run log4net.

Question

Sunday, May 11, 2014 1:37 AM

In my web config I have the standard

connection string.

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-20140510112256.mdf;Initial Catalog=aspnet-ITJobs-20140510112256;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

Now in order to run Log4 net I need to configure or add an entity connection string.

How do I go about doing so?

I am using EF 6 and MVC 5

All replies (8)

Thursday, May 15, 2014 9:54 PM ✅Answered

Hi mcupryk,

Thanks for your reply.

You can try modify this in the connectionstring:

providerName=System.Data.SqlClient 

to:

providerName=System.Data.EntityClient 

More information:

#Explicit connection string for EF

http://blogs.msdn.com/b/rickandy/archive/2008/12/09/explicit-connection-string-for-ef.aspx

Hope this helps you.

Best Regards,

Eileen


Friday, May 16, 2014 12:50 AM ✅Answered

Modify the connection string as 

metadata=res://*/datamodel.csdl|res://*/datamodel.ssdl|res://*/datamodel.msl;provider=System.Data.SqlClient;provider connection string="data source=servernamw \instancename;initial catalog=dbname;user id=username;multipleactiveresultsets=True;App=EntityFramework"


Sunday, May 11, 2014 1:49 AM

Hi!

  1. Attach your aspnet-20140510112256.mdf database into "YourSQLServerComputer"
  2. Change your connection string like this:
<add name="DefaultConnection" connectionString="Data Source=YourSQLServerComputer;Initial Catalog=aspnet-20140510112256;Integrated Security=true" providerName="System.Data.SqlClient" />

Have fun


Sunday, May 11, 2014 10:58 AM

Server Error in '/' Application.


The 'data source' keyword is not supported.

             Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.            
Exception Details: System.ArgumentException: The 'data source' keyword is not supported.
Source Error:

Line 48:         {
Line 49:             // create a entity connection string from the input
Line 50:             EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(entityConnectionString);
Line 51: 
Line 52:             // read the db connectionstring

Sunday, May 11, 2014 8:30 PM

Hi,

Please specify your SQL Server computer name and Instance name. 

This is to write exactly your connection string. For example, if your SQL Server Computer name is "SQLComputer" and SQL Server Instance name is SQLExpress, then your connection string is:

<add name="DefaultConnection" connectionString="Data Source=SQLComputer\SQLExpress;Initial Catalog=aspnet-20140510112256;Integrated Security=true" providerName="System.Data.SqlClient" />

Make sure you have correct syntaxes.

Have fun


Tuesday, May 13, 2014 10:48 PM

Hi ,

In most scenarios, we want to use the same database connection string throughout the application. To accomplish both of these goals I created the following helper class

public static class Log4NetManager
    {
        public static void InitializeLog4Net()
        {
            //initialize the log4net configuration based on the log4net.config file
            XmlConfigurator.ConfigureAndWatch(new FileInfo(System.AppDomain.CurrentDomain.BaseDirectory + @"\Log4Net.config"));

            Hierarchy hier = log4net.LogManager.GetRepository() as Hierarchy;
            if (hier != null)
            {
                // Get ADONetAppender by name
                AdoNetAppender adoAppender = (from appender in hier.GetAppenders()
                                              where appender.Name.Equals("DbAppender", StringComparison.InvariantCultureIgnoreCase)
                                              select appender).FirstOrDefault() as AdoNetAppender;

                // Change only when the auto setting is set
                if (adoAppender != null && adoAppender.ConnectionString.Contains("{auto}"))
                {
                    adoAppender.ConnectionString = ExtractConnectionStringFromEntityConnectionString(
                            GetEntitiyConnectionStringFromWebConfig());

                    //refresh settings of appender
                    adoAppender.ActivateOptions();

                }
            }
        }

        private static string GetEntitiyConnectionStringFromWebConfig()
        {
            return ConfigurationManager.ConnectionStrings["NorthwindEntities"].ConnectionString;
        }

        private static string ExtractConnectionStringFromEntityConnectionString(string entityConnectionString)
        {
            // create a entity connection string from the input
            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(entityConnectionString);

            // read the db connectionstring
            return entityBuilder.ProviderConnectionString;
        }
    }

More information,I recommand that you can read the useful link carefully:

http://www.oakwoodinsights.com/adding-log4net-mvc-site/

Hope this helps you.

Best Regards,

Eileen


Wednesday, May 14, 2014 8:10 PM

yes this is where I got the class.

this is my code:

public static class Log4NetManager
    {
        public static void InitializeLog4Net()
        {
            //initialize the log4net configuration based on the log4net.config file
            XmlConfigurator.ConfigureAndWatch(new FileInfo(System.AppDomain.CurrentDomain.BaseDirectory + @"\Log4Net.config"));

            Hierarchy hier = log4net.LogManager.GetRepository() as Hierarchy;
            if (hier != null)
            {
                // Get ADONetAppender by name
                AdoNetAppender adoAppender = (from appender in hier.GetAppenders()
                                              where appender.Name.Equals("DbAppender", StringComparison.InvariantCultureIgnoreCase)
                                              select appender).FirstOrDefault() as AdoNetAppender;

                // Change only when the auto setting is set
                if (adoAppender != null && adoAppender.ConnectionString.Contains("{auto}"))
                {
                    adoAppender.ConnectionString = ExtractConnectionStringFromEntityConnectionString(
                            GetEntitiyConnectionStringFromWebConfig());

                    //refresh settings of appender
                    adoAppender.ActivateOptions();

                }
            }
        }

        private static string GetEntitiyConnectionStringFromWebConfig()
        {
            return ConfigurationManager.ConnectionStrings["ITJobsEntities"].ConnectionString;
        }

        private static string ExtractConnectionStringFromEntityConnectionString(string entityConnectionString)
        {
            // create a entity connection string from the input
            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(entityConnectionString);

            // read the db connectionstring
            return entityBuilder.ProviderConnectionString;
        }
    }

Wednesday, May 14, 2014 8:12 PM

and it gives me an error on the connection string

The 'data source' keyword is not supported.

             Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.             
Exception Details: System.ArgumentException: The 'data source' keyword is not supported.
Source Error:

Line 48:         {
Line 49:             // create a entity connection string from the input