Emil’s Blog

Programming Windows, .Net, EPiServer and whatnot…

[Powered by WordPress.]

August 26, 2009

Modifying log4net settings

by @ 7:29. Filed under .Net programming

Here's how to modify log4net settings from a config file before initializing log4net. This can be useful for example if you have encrypted connection strings in App.config and have to decrypt them before passing them to log4net.

XmlElement log4NetSection = (XmlElement)ConfigurationManager.GetSection("log4net");

// Modify the xml, e.g. by adjusting the InnerXml property...
log4NetSection.InnerXml = StringCrypto.DecryptTaggedString(mypwd, log4NetSection.InnerXml);

XmlConfigurator.Configure(log4NetSection);

/Emil

August 24, 2009

Best practice for NHibernate: Don’t store references to ISession-instances

by @ 7:35. Filed under NHibernate

Do you see any (or all) of these exceptions (NHibernate.ADOException) in you NHibernate-based web application?

could not execute query
failed to lazily initialize a collection, no session or session was closed
Transaction not successfully started
The server failed to resume the transaction. Desc:86000002dc.

If you do, it might be a good idea to make sure you don't store any ISession-references anywhere. If you do there's a great risk that something goes wrong if two threads (i.e. two requests) are using the session at the same time as the NHibernate session implementation is not thread safe.

Instead, do this when an ISession is needed:

ISession session = SessionFactory.Factory.GetCurrentSession();

That code will get the session from the current context (which of course must be updated by your code, look here for more info).

/Emil

[powered by WordPress.]

jour·nal n. A personal record of occurrences, experiences, and reflections kept on a regular basis; a diary.

Internal links:

Categories:

Search blog:

Archives:

August 2009
M T W T F S S
« May   Sep »
 12
3456789
10111213141516
17181920212223
24252627282930
31  


View Emil Åström's profile on LinkedIn

General links:

I read:

Visitors

Recent Comments

Spam caught

Other:

Clicky Web Analytics

36 queries. 0.979 seconds