We have multiple WCF projects and we want place the all connection strings in one connectionStrings.config file and use it from there. For this I have tried the following:
connectionStrings.config file in parent folder of all WCF projects (path: D:\Projects\connectionStrings.config)
<connectionStrings>
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
web.config file of each WCF Project (path: D:\Projects\SampleWCFProject\web.config)
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings configSource="..\connectionStrings.config"/>
</configuration>
But I get this error:
The configSource attribute is invalid.: The configSource '..\connectionStrings.config' is invalid. It must refer to a file in the same directory or in a subdirectory as the configuration file.
It works if we place the connectionStrings.config in same folder as the web.config or in a child folder. But that's not what we require.
Can anyone help us with this? Thanks in advance.
In my opinion, we could refer to this file by Adding Existing Item.
Then switch the “build action” to “content” and “copy to output directory” to “Always” in the property page of the referring file.
We could choose to add a file to the current project or a link to this file.
Finally, the file in other directory cannot work may be due to problems with read and write permissions to the file.
Feel free to let me know if there is anything I can help with.
When publishing my ASP.Net Website (not a Web Application), the publisher does not include the Web.ConnectionStrings.config file that is next to the web.config. This is required since my web config looks like this:
<connectionStrings configSource="Web.ConnectionStrings.config"/>
How can I get a File System Publish to include files that Visual Studio seems to be ignoring. Please note that this is a website created using [File] > [New Website] in Visual Studio, not a [File] > [New Project] ASP.Net site so Content=Include will not work.
Steps to reproduce:
In Visual Studio: File > New > Website..
Create the Web.ConnectionStrings.config xml document (see ConnectionStrings.config code below).
In the web config link up the Web.ConnectionStrings.config file to the Web.Config file (see Web.config code below)
Publish the website to a folder on your file system, the Web.ConnectionStrings.config doesn't move with the rest of the files.
Web.config:
<configuration>
<connectionStrings configSource="Web.ConnectionStrings.config"/>
..
Web.ConnectionStrings.config:
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
<add name="connString" connectionString="yourConnectionstringhere"/>
</connectionStrings>
The way you publish the website is OK.
But the name of the file into which the connectionstrings get stored must not start with the prefix web., just call it connectionstrings.config instead.
In web.config you put:
<configuration>
<connectionStrings configSource="connectionStrings.config"/>
In the renamed file connectionstrings.config you place:
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
<add name="connString" connectionString="yourConnectionstringhere"/>
</connectionStrings>
I think this article will help
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/deploying-extra-files
Basically you edit the picture .pubxml file to tell it to include additional files during deployment
In an open source ASP.NET application I'm working on, I need to keep certain data in the configuration file private, while still keeping it easy for people to build and debug it on their own machine. This is data such as API keys, Mail Settings, etc..
How would I keep this data separate and out of the git repository while still allowing people to just pull and build without having to set up a bunch of stuff?
In your config file you can define configSource:
<configuration>
<appSettings configSource="filepath1.config" />
<connectionStrings configSource="filepath2.config" />
<!--etc-->
</configuration>
Put the configurations that you need to keep private in a separate config file, then exclude them in your .gitignore.
Keep in mind that this will ignore the whole section and overwrite it with the context you have in the referenced file.
You can also do Configuration Transform, which allows you to only overwrite a small set of variables in sections. For example:
In your main Web.config:
<configuration>
<appSettings>
<add key="Key1" value="Something I dont't Care"/>
<add key="Key2" value="Something dummy"/>
</appSettings>
</configuration>
And in your Web.Release.config:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Key2" value="Something I want to keep secret"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
In this case the "Key2" value that you want to keep private will be in a separate file, and you can exclude the Web.Release.config through .gitignore.
Also there's another approach that I never tried, which can also overwrite config using external file.
How can i specify more than one relative file path in appSetting path attribute in asp.net web.config file. where my appsetting keys are residing in two different file .
<appSettings file="Web.User.config">
</appSettings >
You cannot reference more than one file path for appSettings. appSettings is a section and you cannot have more than one of those. However, you can add a new section which works like the appSettings section. Example:
<configuration>
<configSections>
<section name="CustomConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
....
<CusTomConfig>
<add key="Key1" value="Value1"/>
</CustomConfig>
Then, you can access that section with:
NameValueCollection settings = (NameValueCollection)ConfigurationManager.GetSection("CustomConfig");
A second option is using a Custom Configuration Section. This will require some boilerplate code (as shown in the MSDN documentation) but you will be able to access setting values with properties, rather than referencing them with strings like settings["Key1"].
I have created a simple scenario using Log4net, but it seems that my log appenders do not work because the messages are not added to the log file.
I added the following to the web.config file:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/>
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="D:\MyData\Desktop\LogFile.txt" />
<appendToFile value="true" />
<encoding value="utf-8" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
Within the global ASAX file I have added:
ILog logger = LogManager.GetLogger(typeof(MvcApplication));
And within the Application_Start method:
logger.Info("Starting the application...");
Why the test log "Starting the application..." is not being added to the log file?
Do you call
log4net.Config.XmlConfigurator.Configure();
somewhere to make log4net read your configuration? E.g. in Global.asax:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
// Initialize log4net.
log4net.Config.XmlConfigurator.Configure();
}
Use this FAQ page: Apache log4net Frequently Asked Questions
About 3/4 of the way down it tells you how to enable log4net debugging by using application tracing. This will tell you where your issue is.
The basics are:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
And you see the trace in the standard output
As #AndreasPaulsson suggested, we need to configure it. I am doing the configuration in AssemblyInfo file. I specify the configuration file name here.
// Log4Net Configuration.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Also, Make sure the "Copy always" option is selected for [log4net].config
Make sure the process (account) that the site is running under has privileges to write to the output directory.
In IIS 7 and above this is configured on the application pool and is normally the AppPool Identity, which will not normally have permission to write to all directories.
Check your event logs (application and security) to see if any exceptions were thrown.
Insert:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
at the end of AssemblyInfo.cs file
In my case I had to give the IIS_IUSRS Read\write permission to the log file.
For me I moved the location of the logfiles and it was only when I changed the name of the file to something else it started again.
It seems if there is a logfile with the same name already existing, nothing happens.
Afterwards I rename the old file and changed the log filename in the config back again to what it was.
In my case, log4net wasn't logging properly due to having a space in my project name. Drove me nuts why the same code worked just fine in a different project, but didn't in the new one. Spaces. A simple space.
So, beware spaces in project names. I've learned my lesson.
Make sure the following line code should be there in AssemblyInfo.cs file.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
and also check for this line in Application_start() method.
log4net.Config.XmlConfigurator.Configure();
For me I had to move Logger to a Nuget Package. Below code need to be added in NuGet package project.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]
See https://gurunadhduvvuru.wordpress.com/2020/04/30/log4net-issues-when-moved-it-to-a-nuget-package/ for more details.
Your config file seems correct. Then, you have to register your Log4net config file to application. So you can use below code:
var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));
After registering process, you can call below definition to call logger:
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
log.Error("Sample log");
There are a few ways to use log4net.
I found it is useful while I was searching for a solution. The solution is described here: https://www.hemelix.com/log4net/