I have an application which uses log4net for logging messages. In the web.config there is a section which looks like this:
<log4net>
<appender name="smth" type="smthType">
<file value="name.log" />
</appender>
</log4net>
My question is: how can I get the value name.log from this section?
I tried this:
Dim section = ConfigurationManager.GetSection("log4net")
but since the value is in the appender section, I am not sure how to get it.
Any help would be greatly appreciated
I believe that, instead of reading from web.config, you should use the log4net API, for example:
Dim appender as IAppender = LogManager.GetRepository().GetAppenders()[0];
Related
I am using log4Net with share point 2010. I have a feature which automatically adds the log4net config when my soultion is deployed in Error mode using following code
SPWebService service = SPWebService.ContentService;
service.WebConfigModifications.Clear();
//ADD log4Net config section
service.WebConfigModifications.Add(new SPWebConfigModification()
{
Path = "configuration/configSections",
Name = "section[#name='log4net']",
Sequence = 0,
Owner = CREATE_NAME,
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
Value = string.Format(#"<section name='log4net' type='log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version={0}, Culture=neutral, PublicKeyToken={1}' />", LOG4NET_VERSION, LOG4NET_PUBLIC_KEY_TOKEN)
});
string log4netConfig = #"<log4net>
<appender name='RollingFileAppender' type='log4net.Appender.RollingFileAppender'>
<file value='C:\\logs\\Logger.log' />
<appendToFile value='true' />
<rollingStyle value='Composite' />
<datePattern value='yyyyMMdd' />
<maxSizeRollBackups value='200' />
<maximumFileSize value='50MB' />
<layout type='log4net.Layout.PatternLayout'>
<conversionPattern value='%d [%t] %-5p %c [%x] <%X{auth}> - %m%n' />
</layout>
</appender>
<root>
<level value='ERROR' />
<appender-ref ref='RollingFileAppender' />
</root>
</log4net>";
//add error default config
service.WebConfigModifications.Add(new SPWebConfigModification()
{
Path = "configuration",
Name = "log4net",
Sequence = 0,
Owner = CREATE_NAME,
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
Value = log4netConfig
});
service.Update();
service.ApplyWebConfigModifications();
I wanted to create another feature which over writes Error mode of the log4net and change it to debug, so that the end user does not have to modifiy web config manually.
The problem is when the second feature is activated, it deletes everything added by the first feature.
Is this standard behaviour ?? Any feature that would activate would delete the changes by other feature.
EDIT 2
Steps to replicate
Create 2 feature. both of them should add some different entries in web config.
Activate feature 1 - Feature 1 changes are in web config
Activate feature 2 - Feature 2 changes are in web config but feature 1
changes are gone
Deactivate both the features
Activate feature 2 - Feature 2 changes are in web config
Activate feature 1 - Feature 1 changes are in web config but feature
2 changes are gone
The reason why the first web configuration is removed is because of the code block:
service.WebConfigModifications.Clear();
Basically you're saying that you will be clearing all other configurations set in your web config prior to adding the items in your feature. Removing the said code block should fix your issue.
If you need to do some other things when activating or deactivating a feature try using Feature Receivers. A good example to start with web configurations and feature receivers can be found here.
Also to give you more idea on the code block above is giving you problems you can check out the problem another guy which had the similar problem here.
if I understand this correctly, what's happening is you add a webconfigmodification, this isn't technically "added" yet as it is hasn't been implemented just yet. When you created the 2nd modification, it overwrites the first modification you created, which in turns, erases the first modification that was made, and this was the one that was applied.
One of my goals in creating the bootstrap project is to set a default log location. I would like the location to be based on the local app data folder. I cannot figure out how to reference the builtin Burn variable LocalAppDataFolder. I have found information about how to reference these variables in code, but not in the xml.
The reference to the property looks like this:
<MsiPackage SourceFile="MyInstaller.msi" LogPathVariable="[LogLocation]" />
The property is set like this:
<Variable Name="LogLocation" Value="[LocalAppDataFolder]MyLogFolder\Setup" Type="string"/>
The log output shows:
Initializing string variable 'LogLocation' to value
'[LocalAppDataFolder]MyLogFolder\Setup'
What am I missing to resolve [LocalAppDataFolder] ?
thanks in advance.
It's normal for the log output to show the un-formatted value, so that part looks correct. I think what you are missing is LogPathVariable should be specified without the brackets.
<MsiPackage SourceFile="MyInstaller.msi" LogPathVariable="LogLocation" />
I need to use the percentage char within the value attribute of the XmlFile element to configure path of log4net rolling file appender while application is installed.
Target is to get log4net configured like this
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Log_[%processid].log" />
...
</appender>
The base wix code looks like this:
<util:XmlFile
Id="RollingFileAppenderLogPath"
File="[INSTALLLOCATION]log4net.config" Action="setValue" Permanent="yes"
ElementPath="/log4net/appender[\[]#name='RollingFileAppender'[\]]/file" Name="value"
Value="[LOGPATH]Log_[%processid].log"/>
I've tried some various replacements for [ ] and % like entities ([, ] and %), doubling, tripling, quadrupling but the value is always mentioned as environment variable or causes ICE03: Invalid string format.
The replacement result looks like this:
<file type="log4net.Util.PatternString" value="<inserted LOGPATH>Log_.log"/>
Is there a way to get [%processid] forced as string to get it inserted as intended?
Think I found the solution: the problem is with the square brackets!
If you open the WIX Documentation and navigate to the "XmlFile Element", on the "Value" property it reads:
The value to be written. See the Formatted topic for information how to escape square brackets in the value.
So, just ckeck the link above and alter the text, escaping the square brackets and all should work fine! :)
I'm using the json plugin that comes with struts 2 (json-lib-2.1.jar) and trying to follow the website to set it up.
Here's my struts.xml
<struts>
<package name="example" extends="json-default">
<action name="AjaxRetrieveUser" class="actions.view.RetrieveUser">
<result type="json"/>
</action>
</package>
</struts>
but I get this warning:
SEVERE: Unable to find parent packages json-default
Is there something else I'm supposed to do?
Edit:
I added this method to my RetrieveUser:
public Map<String,Object> getJsonModel()
{
return jsonModel;
}
And my struts.xml looks like this:
<struts>
<package name="example" extends="json-default">
<action name="AjaxRetrieveUser" class="actions.view.RetrieveUser">
<result type="json"/>
<param name="root">jsonModel</param>
</action>
</package>
</struts>
However, I don't think the response is going from the RetrieveUser class to the javascript. I'm using firebug and no request gets sent.
I believe that net.sf.json-lib is just a toolset you can use in your Java to build up JSON-ready objects, suitable to be returned by actions such as you describe.
Probably, you need to include struts-json-plugin - make sure its version matches your struts version.
I notice also that as written, your action will attempt to return RetrieveUser, serialized. Most implementations I've done/seen specify the root object to be returned, by adding
<param name="root">jsonUser</param>
Under the tag, and define this method in RetrieveUser
public Map<String, Object> getJsonUser()
[This is mentioned in the Sruts2 doc]. Hope that helps.
[edit] I use Map - you could also use the object structures provided by json-lib instead.
Re: Your edit. Probably need to see your calling javascript. And probably I will suggest that you make sure you have both a success and an error handler. Can you debug/log to show that the method is being called in java ? Do your logs show anything ? This is usually some sort of error....
I am adding elements to an XML file during installation using the XmlFile element:
<util:XmlFile Id="SetOracleDialectProperty"
Action="createElement"
ElementPath="//hibernate-configuration/session-factory"
Name="property"
Sequence="9"
File="[INSTALLLOCATION]Config\hibernate.config"
Value="NHibernate.Dialect.Oracle10gDialect"/>
The empty file I am writing to looks like this:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
</session-factory>
</hibernate-configuration>
After running the installer I end up with this:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property xmlns="">NHibernate.Dialect.Oracle10gDialect</property>
</session-factory>
</hibernate-configuration>
The problem is that the empty xmlns attribute is overriding the xmlns specified in the root node of the file so the property element is not recognised correctly by nhibernate.
How can I either set the value to match the root node or remove the xmlns attribute?
I have spent some time searching for an answer and the closest I've found is "do what you would do in MSXML" which doesn't help me as it doesn't say how to do it in WiX (e.g. what attribute on XmlFile to use).
EDIT
To explain Rob's answer slightly, in a place where I can use nice formatting:
You add a document fragment by setting Node="document" on the XmlConfig element.
You have to explicitly set the namespace otherwise you get the default one again.
Also although you're adding a "document" it doesn't seem to work if you specify more than one element. You get a mysterious and thoroughly unhelpful "Setup wizard ended prematurely" runtime error.
So my fixed code looks like this:
<util:XmlConfig Id="MsSqlDialect"
Action="create"
ElementPath="//hibernate-configuration/session-factory"
File="[INSTALLLOCATION]Config\hibernate.config"
Node="document">
<![CDATA[
<property xmlns="urn:nhibernate-configuration-2.2" name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
]]>
</util:XmlConfig>
I know this is years later but if anyone else comes across this I think the true solution is this:
<util:XmlFile Id="SetOracleDialectProperty"
Action="createElement"
ElementPath="//hibernate-configuration/session-factory"
Name="urn:nhibernate-configuration-2.2:property"
Sequence="9"
File="[INSTALLLOCATION]Config\hibernate.config"
Value="NHibernate.Dialect.Oracle10gDialect"/>
change is from Name="property" to Name="urn:nhibernate-configuration-2.2:property" - when config is written it will apprear as just as it will recognize it is the default namespace. I had the same problem adjusting manifest files and this approach sorted it.
The problem here is that MSXML states that createElement will always give you the default namespace (just as you are seeing). I think you'll need to switch to the more complex but more powerful XmlConfig. In this case, try using a document fragment to add the entire element with correct namespace instead of depending on MSXML to create it for you.