Writing to the ASI log files (log4net)

I can see (*cough* Reflector *cough*) that log4net is referenced many places through the iMIS app, and appears to be responsible for writing Asi.Webroot.log and possibly other files.

  • Where can I find basic information for using log4net?
  • Are there any code samples, or specific examples easily found in code?
  • Is there an ASI wrapper for this, or do I need to invoke log4net directly?
  • Is it possible for me to invoke log4net in a way that adds my entries to Asi.Webroot.log?
  • Is it advisable to do so?  (That is, it won't cause concurrency problems with iMIS writing to it also.)

I'm assuming that my app would run inside the Net or iMIS_Public folder, and be a part of the same application space with iMIS.

I can make a business case for writing to ASI's log, especially for product-ized software.  It would also be useful to write private log files, to aid in debugging without cluttering up the main log.

 

-- Bruce

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Found log4net's home

I found log4net at http://logging.apache.org/log4net/, so now I have a place to read the generic docs.

The specific questions about working safely with iMIS are still open.

-- Bruce

Getting access to the log

Getting access to the log and writing to it from within either the appserver or public view application is as simple as: 

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

You can then call the log object's methods (Error, Warn, etc) as normal for log4net, and it will automatically log to the same log file iMIS is using. You do not need to do anything regarding locking or synchronizing; all of that is handled internally by log4net.

However, note that starting in 15.1 we are starting to move away from log4net in favor of ASP.NET Health Monitoring (which is filterable and more easily redirectable than log4net is, as well as tying into Windows Health Monitoring). There are several base classes in Asi.Providers.HealthMonitoring that can be used for this purpose (or you can roll your own monitoring events, or both):

AsiWebFailureAuditEvent failureEvent = new AsiWebFailureAuditEvent(
    ResourceManager.GetPhrase("AccountCreationFailed", "Failed to create new account {0}: {1}.", new string[] { username, ex.Message }),
    this, AsiAuditEventCode.AccountCreation);
failureEvent.Raise();

AsiWebErrorEvent errorEvent = new AsiWebErrorEvent(
    ResourceManager.GetPhrase("AccountCreationFailed", "Failed to create new account {0}.", new string[] { username }),
    this, AsiErrorEventCode.Error, ex);
errorEvent.Raise();

By default, Health Monitoring error and audit failure events are logged to the Windows Application Event Log, although customers can modify these rules (for example, to log additional events such as audit success, or to log to a different event sink, such as SQL Server).

log4net continues to exist and work in 15.1 if you need to support both current and older versions from the same codebase, however.