Dynamically change log target file using Enterprise Logging - c#

So I'm trying to use enterprise logging in an application, and I want it to have multiple files, so far I have the following in my app.config file:
<add name="Normal" fileName="C:\MyApp\Logs.log" .../>
And in my code I'm simply using
Logging.Write("My log here");
Now what if I want to programatically change the file where it's being logged to C:\MyApp\MyDateHere_Logs.log, how could I approach this?
Haven't found a lot of solutions online. Thanks beforehand.

In the end I ended up creating a custom trace listener of my own and then changing the filename attribute on runtime by following the guide here.
You can access the attribute which stores your path by using Attribute["filename"] (assuming the attribute in your config file's trace listener node has the path in an attribute named "filename".)

Related

How to read and write custom sections in app.config?

I want to read and write the users settings in the app.config file of my project.
I want to add the user name as a section and adding its setting, for each user if he is not exist in the app.config.
And I want to read it at the beginning of the application.
Can you help me please?
.Net already has this class ConfigurationManager you can use to read app.config and web.config for web scenarios. You could use something like this
var temp = ConfigurationManager.AppSettings["data"];
for more info please check the MSDN ConfigurationManager

Store user settings file in executables directory

I'm developing a software in .NET 4.0, which reads and writes application settings. On finish the software will be stored on a file server. No local instances will be executed.
By default the user settings XML file is stored in every users AppData\... directory, but I want to change the file location to the same directory the executable is stored.
This means, that all users should use the same XML user-settings file with modified contents.
I've read this question and answeres where a user describes how to realize that in JSON format.
Question:
Isn't there any other (simple) way to tell the Settings class, where to read from and write to user settings?
The following has been discussed:
Users will always have enough access rights to modify the settings file.
Modifications on settings should be picked up by other users.
Users will start the application from different network computers.
I could implement my own XML file handled by the application (I'll keep this in mind).
I'm not sure if you can get the functionality that you want with the standard Settings class. But I do think that the end result your searching for can be accomplished.
If you want changes in settings user 1 makes to be instantly enforced for user 2, you should look at storing the settings in a database. Your application can then be periodically check this table for changes. For instance if user 1 changes the color of a control, then every time user 2 loads a screen with that control you check the database for the color.
Or, if you want the settings to be applied on start-up of you application. Use a datacontract + xml serializer to write settings to a file of your choosing on a network accessible path/folder. Then make sure you can handle read/write locking of this file.
These are just general ideas that I think you should consider. I dont claim these are your only options though. If you whish to pursue any of these things there are a bunch of blogs and stackoverflow pages with all the information you need.
good luck!
I added a custom Application Configuration File called AppSettings.config to my project and set its Copy to output property to Copy if newer (this copies the config file into the executables folder on building the project.
My settings must be classified as appSettings, not applicationSettings (non-editable) or userSettings (stored in the users AppData folder). Here's an example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- single values -->
<add key="Name" value="Chris" />
<add key="Age" value="25" />
<!-- lists, semi-colon separated-->
<add key="Hobbies" value="Football;Volleyball;Wakeboarding" />
</appSettings>
</configuration>
Look at the code below, how to access and modify them:
Configuration AppConfiguration =
ConfigurationManager.OpenMappedExeConfiguration(
new ExeConfigurationFileMap {ExeConfigFilename = "AppSettings.config"}, ConfigurationUserLevel.None);
var settings = AppConfiguration.AppSettings.Settings;
// access
var name = settings["Name"].Value;
var age = settings["Age"].Value;
var hobbies = settings["Hobbies"].Value.Split(new[]{';'});
// modify
settings["Age"].Value = "50";
// store (writes the physical file)
AppConfiguration.Save(ConfigurationSaveMode.Modified);
I used the built-in app.config for default application settings and wanted to separate them from the editable global settings in AppSettings.config. That's why I use a custom config file.
That's not directly an answer to the question, but it solves the problem on how to store and share editable application settings in custom configuration files.
Hope it helps any other users! :-)

Create asp.net website with editable config file

I want to create setup of my web application with editable web.config file content.
Any help?
If you are trying to change the config with your code, the following should help. Any time you execute the following code, your site will be restarted. So keep that in mind.
var config = (Configuration)WebConfigurationManager.OpenWebConfiguration("~");
var configSection = (MySectionTypeHere)myConfiguration.GetSection("system.web/section");
//make your edits here
myConfiguration.Save();
If this is not what you're looking for, perhaps you could provide some more information.
You could use a tool like MS Web deploy. Read more about it here. http://learn.iis.net/page.aspx/578/package-an-application-for-the-windows-web-application-gallery/

Custom errors not working in Sharepoint 2010

I've included the line customErrors mode="On" in my web.config file for my Sharepoint site, but I am still not getting any error messages. It just keeps telling me to include it when it errors out.
I am unsure at this point what else to do, any help would be appreciated, thanks!
The SharePoint application is consists of a number of virtual directories mapped together. You can change root web.config under C:\inetpub\wwwroot\wss\VirtualDirectories\ but that can be and does get overwritten by a number of mapped virtual folders, i.e. _controltemplates, _layouts, _vti_bin, _wpresources.
If you are failing on a system pages, you may need to go and modify the web.config files in the mapped folders instead.
As you mentioned ULS usually contains all the errors, and SharePoint ULS Logviewer is indispensable tool here, http://sharepointlogviewer.codeplex.com/.
Another solution for this I found is using a tool called ULS Viewer, it can directly access the Sharepoint log files and will show you the exception.
http://archive.msdn.microsoft.com/ULSViewer
It can be downloaded from the link above!

Best way to implement file access in C#

Scenario - I need to access an HTML template to generate a e-mail from my Business Logic Layer. It is a class library contains a sub folder that contains the file. When I tried the following code in a unit test:
string FilePath = string.Format(#"{0}\templates\MyFile.htm", Environment.CurrentDirectory);
string FilePath1 = string.Format(#"{0}\templates\MyFile.htm", System.AppDomain.CurrentDomain.BaseDirectory);
It was using the C:\WINNT\system32\ or the ASP.NET Temporary Folder directory.
What is the best to access this file without having to use an app.config or web.config file?
[This is using a WCF Service]
You're running this from an ASP.Net app right? Use Server.MapPath() instead.
Also take a look at System.IO.Path.Combine() for concatenating paths.
[Edit]
Since you can't use System.Web, try this:
System.Reflection.Assembly.GetExecutingAssembly().Location
Or GetEntryAssembly().
I belive you are looking for
System.IO.Path.GetDirectoryName(Application.ExecutablePath);
I got this working on my site! I haven't been working on just this for the last 2 years!!!
I added this inside my system.serviceModel block of the web.config. It seems to make all the paths relative to where your service is installed.
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
better late than never?
Use
System.Web.HttpServerUtility.MapPath( "~/templates/myfile.htm" )

Categories

Resources