Controlling settings for serilog in app.config - serilog

I need to log events in Json format using Serilog. I want to use RollingFileSink.
According to the following link, we cannot specify TextFormatter in App.config file.
How to specify JsonFormatter in Web.config for SeriLog?
Is is possible to specify all other configurations like file path in app.config and then add textformatter as JsonFormatter in code while initialing the logger?

Just using plain settings works well for this, e.g.:
<add key="filePath" value="C:..." />
And:
WriteTo.RollingFile(..., ConfigurationManager.AppSettings["filePath"])

Related

Web.config transformation for different websites

I have one project for publishing two separate websites on different servers. Both websites is in different languages (En and tr).
For web-deploy I want to change appsettings for different environments for Debug and Release (For both websites).
I added config files for transformations like this:
Appsetting in main Web.config:
<add key="WebsiteMainCulture" value="en-US"/>
The code in transformation files are accordingly like this:
<appSettings>
<add key="WebsiteMainCulture" value="TR"
xdt:Locator="Match(key)" xdt:Transform="Replace"/>
</appSettings>
I selected Web.TR.Debug.config and ran project, But I always get 'en-US' as WebsiteMainCulture.
string website_main_culture = ConfigurationManager.AppSettings["WebsiteMainCulture"].ToString(); // always en-US
Please help. Is there any better approach suggested for same environment?
Thanks!
I got the solution for the problems I asked, also related to my previous question here:
https://stackoverflow.com/questions/30612865/selecting-correct-seed-method-for-particular-language
I just added config transformation files by right clicking, to relevant publish(web deploy) files.Then added desired values to keys. And it worked.
Here are config transformation file auto generated.

Programmatically enable or disable anonymous authentication in IIS

I have a web application and I need to provide its users the option to switch login method from FormsAuth to WindowsAuth. I managed to change the web.config file via code:
Configuration config = WebConfigurationManager.OpenWebConfiguration(Url.Content("~"));
AuthenticationSection auth = ((AuthenticationSection)(config.SectionGroups["system.web"].Sections["authentication"]));
auth.Mode = AuthenticationMode.Windows; // Or Forms if I want to.
config.Save();
But the problem is, when I use FormsAuth, I need the Anonymouse Authentication option to be turned on, and when I use WinAuth, I need it to be off. And I just cannot find the way to change that option via code.
Everything on the internet says to do this:
<security>
<authentication>
<anonymousAuthentication enabled="false/true" />
</authentication>
</security>
But when I insert this into my webapp's web.config it says that configuration is wrong. Than I read this might work in another config file, like appHost.config or something like that but I prefer to make changes only to my own application and not to IIS I hope you understand why.
So, how can I do that?
You are trying to update wrong section. anonymousAuthentication is part of system.webServer and not system.web. Correct configuration is
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
You can modify it using ServerManager class found in Microsoft.Web.Administration. Search for nugget package "Microsoft.Web.Administration". Once you have added reference to Microsoft.Web.Administration.dll using nugget you can modify it using code as follows:
using (ServerManager serverManager = new ServerManager())
{
Microsoft.Web.Administration.Configuration config = serverManager.GetApplicationHostConfiguration();
Microsoft.Web.Administration.ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication");
anonymousAuthenticationSection["enabled"] = true;
serverManager.CommitChanges();
}
Okay, so, turned out I could not find a way to dynamically change the auth mode, but I found pretty neat solution to the problem: I have created another web application, nested it inside the first one, had set Forms Auth mode for the first one and Windows for the nested and whenever I needed to use IIS's power to work with domain, user groups etc, I used a redirect from one to another, passing data by cookies and as url parameters.

Quartz .net configuration with code instead of config file

I am using Quartz .net with an AdoJobStore jobStore. In the .config file, there is a property that is used to set the connection string:
<add key="quartz.dataSource.default.connectionString" value="Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True" />
However, I manage all my connection strings in a separate config file, so I currently need to dupplicate this connectionString information.
Is there a way to not specify the quartz.dataSource.default.connectionString in the .config file and manually set it via code, so I can get my value in my global connectionStrings config file?
It seams that it is possible to use a NameValueCollection when instanciating a StdSchedulerFactory, but I don't want to manage all settings, only the connectionstring.
In the end, they are all simply named-value pairs.
You can have "most" of them in an .xml file...then "add in" the ones you want via code.
Or have all of them in code.
See the UnitTests for the source code, and you'll see this fairly clearly.
Something like this:
NameValueCollection config = (NameValueCollection)ConfigurationManager.GetSection("quartz");
config.Add("MyCodedUpKey", "MyCodedUpValue");
I would NOT put
"quartz.dataSource.default.connectionString"
in your .xml file...to avoid a collision..... Or you can write extra logic to remove and add to the
NameValueCollection .
Or take a peek here:
Check if Key Exists in NameValueCollection
Finally fix my problem by passing all the configuration attribute using code, but by parsing the quartz config to get them automatically.
And I manually add the quartz.dataSource.default.connectionString property at the end and setting the value by getting it in my global connectionStrings.config file.

Struts2 - Difference between struts.xml and struts-plugin.xml?

I have confusion about these files in struts2.
Normally struts.xml file the core which has configuration in struts1. So I also thought of using struts.xml file.
But in my project already they put struts2-config-browser-plugin.jar, it has struts-plugin.xml. Just searched in Google and found that struts-plugin.xml is enough to run struts application.
Now I'm adding the struts.xml file, the application is not working.
Can I use both XML file in application?
If I remove the struts2-config-browser-plugin.jar , what are the changes need to add in struts.xml file ?
There are three configuration files which are by default loaded by the framework (if they exist):
struts-default.xml - included in struts2-core.jar, contains all basic configuration of the framework
struts-plugin.xml - if plugin want to override some defaults or define its own settings (results, actions, etc)
struts.xml - contains user defined configuration, mostly actions, results and custom stacks of interceptors
You should just use struts.xml and put all the configurations there.
Some notes about struts-plugin.xml - http://struts.apache.org/development/2.x/docs/plugins.html

remove application name from URL

my site uses JSF and the url appears to be, http://mysitename.com/wompower6/faces/home.xhtml
I am using prettyfaces, so if I use the following in pretty-config.xml, i can change the name to http://mysitename.com/wompower6/home
<url-mapping id="home">
<pattern value="/home" />
<view-id value="/faces/home.xhtml" />
</url-mapping>
my questions are
how can i remove the application
name wompower6 , so that the url
becomes mysitename.com/home ?
in my web.xml, i have
<welcome-file>home.xhtml</welcome-file>,
but this does not seem to work. When
i type, mysitename.com, it does not
get mapped to home.xhtml. any clue
here?
how can i remove the application name wompower6 , so that the url becomes mysitename.com/home?
This is a webapp <Context> setting and configuration is dependent on the servletcontainer used. If you're for example using Tomcat, then there are basically 2 options to make your webapp the root webapp.
Rename the WAR file to ROOT.war and Tomcat will by default deploy it on context root.
Set path attribute of <Context> element in Webapp/META-INF/context.xml (or Tomcat/conf/server.xml, depending where you'd like to define it) to an empty String. E.g.
<Context path="" ...>
Other containers support similar constructs. Consult their documentation for detail. If you're using an IDE like Eclipse, then you can also set it in the Web Project Settings property of the project properties (rightclick project and choose Properties). Set the Context root value to just /.
in my web.xml, i have home.xhtml, but this does not seem to work. When i type, mysitename.com, it does not get mapped to home.xhtml. any clue here?
I assume that you're talking about the <welcome-file> setting. This has to point to a physically existing file, not to a virtual URL, such as /faces/*. There are basically two ways to overcome this:
Provide a physically existing /faces/home.xhtml file (it can even be left empty).
Replace the ugly /faces/* URL pattern of the FacesServlet mapping in web.xml by *.xhtml so that it will just kick in on every request for a XHTML file.
<url-pattern>*.xhtml</url-pattern>
This way you don't need to fiddle with /faces/* URL patterns.

Resources