locked
How to replace log4net with Nlog RRS feed

  • Question

  • User-205822839 posted
    Hi, I was using log4net in framwork 2.0, and it was working fine. Now i am building a app in framwork 4.0 and it is giving an error.
    
    
    
    
    This is the code i am using.
    public class DBHelper : IDisposable
    {

    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(DBHelper));
    private const string EDK = "11d50a3a";
    private static Hashtable connStrings = new Hashtable();
    private const bool m_bUseDummyReader = true;
    DbTransaction transaction;
    IDbConnection connection;

    private string databaseName;
    private Database CurrDB;

    cfsEncryption cfsEncrypt = new cfsEncryption();

    public DBHelper(string strDB)
    {
    Init(strDB, IsolationLevel.ReadCommitted);
    }
    private void Init(string strDB, IsolationLevel iso)
    {

    databaseName = strDB;
    try
    {
    CurrDB = DatabaseFactory.CreateDatabase(strDB);
    connection = CurrDB.CreateConnection();
    connection.Open();
    transaction = (DbTransaction)connection.BeginTransaction();

    }
    catch (Exception ex)
    {
    if ((log.IsErrorEnabled))
    log.Error(string.Format(System.Globalization.CultureInfo.CurrentCulture,
    "Connection to DB ->{0} Failed", strDB), ex);

    throw;
    }
    }
    Tuesday, December 27, 2011 12:35 PM

All replies

  • User465171450 posted

    Can you also post the error that you are receiving?

    Tuesday, December 27, 2011 12:36 PM
  • User-205822839 posted

    Could not load file or assembly 'log4net' or one of its dependencies. An attempt was made to load a program with an incorrect format.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.BadImageFormatException: Could not load file or assembly 'log4net' or one of its dependencies. An attempt was made to load a program with an incorrect format.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


    Assembly Load Trace: The following information can be helpful to determine why the assembly 'log4net' could not be loaded.

    WRN: Assembly binding logging is turned OFF.
    To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
    Note: There is some performance penalty associated with assembly bind failure logging.
    To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
    


    Stack Trace:

    [BadImageFormatException: Could not load file or assembly 'log4net' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
       System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
       System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +39
       System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks) +132
       System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +144
       System.Reflection.Assembly.Load(String assemblyString) +28
       System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +46
    
    [ConfigurationErrorsException: Could not load file or assembly 'log4net' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
       System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +618
       System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +209
       System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +130
       System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +178
       System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +94
       System.Web.Compilation.BuildManager.CallPreStartInitMethods() +332
       System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +677
    
    [HttpException (0x80004005): Could not load file or assembly 'log4net' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
       System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9078892
       System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97
       System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +256
    
    Tuesday, December 27, 2011 12:40 PM
  • User1950861088 posted

    Hi, I was using log4net in framwork 2.0, and it was working fine. Now i am building a app in framwork 4.0 and it is giving an error.
    
    
    
    
    This is the code i am using.
    public class DBHelper : IDisposable
    {

    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(DBHelper));
    private const string EDK = "11d50a3a";
    private static Hashtable connStrings = new Hashtable();
    private const bool m_bUseDummyReader = true;
    DbTransaction transaction;
    IDbConnection connection;

    private string databaseName;
    private Database CurrDB;

    cfsEncryption cfsEncrypt = new cfsEncryption();

    public DBHelper(string strDB)
    {
    Init(strDB, IsolationLevel.ReadCommitted);
    }
    private void Init(string strDB, IsolationLevel iso)
    {

    databaseName = strDB;
    try
    {
    CurrDB = DatabaseFactory.CreateDatabase(strDB);
    connection = CurrDB.CreateConnection();
    connection.Open();
    transaction = (DbTransaction)connection.BeginTransaction();

    }
    catch (Exception ex)
    {
    if ((log.IsErrorEnabled))
    log.Error(string.Format(System.Globalization.CultureInfo.CurrentCulture,
    "Connection to DB ->{0} Failed", strDB), ex);

    throw;
    }
    }

    Just replace  

                 "private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(DBHelper));"

    BY

                 "private static readonly NLog.Logger log = NLog.LogManager.GetLogger("DBHelper", typeof(DBHelper));"

    and hurray your problem is solved.

    Tuesday, December 27, 2011 1:18 PM