Custom filename in a rolling Log4Net logfile? - asp.net

We have a ASP .Net application whereby we use Log4Net to log details within the app - nothing new there - and the rolling log filenames are in the usual format of:
rolling-log.txt
rolling-log.txt.1
rolling-log.txt.2 etc.
A each user of the application adds to the logfile, the logfile can be difficult to read for a specific user's case and so, we'd like to modify the config file somehow to record the user's log details individually, each writing to a specific file, e.g.
<applicationId>rolling-log.txt
<applicationId>rolling-log.txt.1
<applicationId>rolling-log.txt.2
etc.
where is each user's unique application Id, made up of a five digit number, e.g.
12345rolling-log.txt
Any ideas on the best way to implement this, assuming that it's possible?
Cheers
Brett

<file type="log4net.Util.PatternString">
<conversionPattern value="C:\Logs\log-%date{ yyyy.MM.dd.HH.mm.ss}-[%processid].log" />
</file>
from here

look for log4net Context Properties...
in your code :
log4net.GlobalContext.Properties["id"] = "12345";
then
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("configPath"));
in the log4net config file:
<file type="log4net.Util.PatternString"
value="%property{id}rolling-log.txt" />

I believe the Log4Net configuration file supports environment variables (e.g. USERNAME) as well as customizable patterns which should give you what you want.
Look at "PatternString for pattern based configuration" in the Log4Net V1.2.10 release notes.

I don't think that log file per user is possible but you can write custom layer between log4net and your application which prepends user id before writing to log.

You can write a custom layout by inherit from XmlLayoutBase.

Checkout the RollingPatternFileAppender, just like the rollingfileappender plus dynamic file name
http://mysite.verizon.net/vze14bhji

Related

How to change previous date log file permision to read only in Log4Net?

I am using Log4Net in my window application, Now i want to change the permission of my all previous date logs to read only automatically and only current date log file will remain same or not changed. How can i do this using Log4Net or is it possible in any other plugin like Elmah ?
This is not possible out of the box. You can invest to extend the file appender you are using to make the file ReadOnly when it is rolled. You can see the find the log4net source code here. Look for the appenders and then extend/ or copy that appender in you project. Make the changes and then use your own appender in the configuration.

How to generate machineKey in web.config using wix?

I need to generate machineKey in my web application installer and put it into the application's web.config. How can I do this?
We need to split the answer in two:
Generate the Machine Key: You will need to implement a custom action to generate the Machine Key (There are many tutorials about creating custom actions so I won't cover that here, review the links below). The important part is the code to generate the Key, review these links: C#, Powershell. You can store the result on an installer property, you may need to make it a secure property to avoid it to apear on the installer logs.
Add the value to the Web.config: Now that you have the key, you can use some of the wix custom actions to modify the web.config, you can use XmlConfig or XmlFile. With this you will be able to modify the Xml file to add the machineKey node using the property created on the previous step. Review the links below for reference on how to use these to update the configuration file.
IMPORTANT: The machineKey element is only valid in the Web.config file at the root of your application and is not valid at the subfolder level.
Additional links:
Adding a Custom Action
Editing Web.Config Connection string settings with Wix
Custom actions with C#
How to pass parameters to the custom action?

dynamic web.config appsetting

I have a web application that we publish and provide for users to install and run on their local network. They choose whether they run it as an internet, or intranet application.
When we publish the application we label the folder with the publish date (e.g. 20140815) so we know which version they have. However, when they contact support, it is a pain for them to get on the server to see the folder information.
I want to add an appsetting for cloudVersion and display it on the license page so they can easily provide it from wherever if needed. Also, they feel better seeing the version increase so they know they are getting value with the service contract.
<add key="cloudVersion" value="20140815"/>
I would like to automate the version in the appsettings. I was hoping I could use a Web.Config transform to set it with a dynamic yyyymmdd, but can't see how to do that and have found nothing in my web search.
<appSettings>
<add key="cloudVersion" value="GETDATE()" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
Is this possible? If so, how? If not, is there a better approach to achieve the same result?
Short of finding a great solution for this, I have elected for the following:
After publication, I run a bat file that does the following:
copy the published directory and rename with today's date
update the Web.Config appSetting value
compress the updated directory using 7zip
upload the compressed directory to Google Drive so my support team can provide it to our users.
You can see how I modified the string here:
bat file to modify web.config setting

ASP.NET - Have settings in the Web.config (and access them using ConfigurationSection) or in a separate XML file

I have few settings which I could place in a separate XML file and have them accessed in the Web app. Then I thought (thinking of one additional file to deploy), why not have them in the web.config itself. However, just because I need to have custom nodes, I can not have the settings under . So, I am thinking of creating a custom config handler following this. Would that be better than having a separate XML file? Is It going to be an overkill or performance wise? Is there a better way to go?
From performance standpoint putting custom settings in web.config and creating a configuration handler will be OK because the config values are cached and read only once when the application starts. Putting the values in a separate XML file you will need to handle the caching your self if you want to avoid parsing it every time you need to access those values.

Using web.config to store and map info

I've been reading others questions regarding storing settings in the web.config. Most of you agree that's a good way to store settings.
I just have another doubt about that.
In my app, I need to map some ID's in the web.config. For example:
[Table = UserType]
1 - User
/2 - Admin
Is it a good idea to store these settings in the web.config so I know what is the right ID in my application? Do you have a better solution?
Thanks guys,
G
If that values doesn't change too often, it's better to create a enum to store that values. An enum sample could be:
enum UserType
{
User = 1,
Admin = 2
}
For more options, also take a look into [Flags] attribute: Enums, Flags, and C# — Oh my! (bad pun…)
Keep in mind every time you edit your web.config file, your IIS application pool get recycled.
I typically use the web.config to store things like connection strings, or key/value pairs that rarely [or never] change.
What you described would be ideal for an enum or perhaps a look up table in the database.
In my web application I have a number of "Configuration" settings that exceed the structure of the Web.Config file, and don't want the web site to restart after changing them.
I ended up creating a number of xml config files where the xml data maps to objects that get cached into collections. This allows us to change the config on the fly, while not restarting the web site. We have a filewatcher that triggers a reload of the cache objects from the Xml files when something in the configuration directory gets modified.
You can also use the database for this obviously, but in our case this was configuration data that is maintained by development and deployed with the build.

Resources