locked
Good logging library required for recording error, exception and program action activity RRS feed

  • Question

  • User264732274 posted

    i need a good logging library by which i could store data in any where like in text file, xml file or database with minimum code and config entry. if i choose xml file for storing log data then xml file will be created as per date wise. first it will check 20150410.xml file exist if not then will create and log data.......if exist then open the xml file and save log data.

    can anyone guide me few best logging library which could solve my purpose.

    Friday, April 10, 2015 2:46 PM

Answers

  • User1508394307 posted

    Take a look at ELMAH which can save logs in xml or database. It saves every error in single xml but you could sort files by date of creation or navigate through using its web interface.

    The configuration is simple as

    <configSections>
    <sectionGroup name="elmah">
      <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
    </sectionGroup>
    </configSections>
    ...
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
    </httpModules>
    ...
    <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
    </modules>
    </system.webServer>
    <elmah>
    
    <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />
    
    </elmah>
    ...
    <system.webServer>
      <handlers>
        <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
      </handlers>
    </system.webServer>

    This will configure storage in /App_Data folder and you could browse errors via /elmah.axd URL. 

    There is much more you could configure, e.g. security, filtering of errors, etc. - you could find more information on ELMAH's website.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 12, 2015 6:20 AM

All replies

  • User1508394307 posted

    Take a look at ELMAH which can save logs in xml or database. It saves every error in single xml but you could sort files by date of creation or navigate through using its web interface.

    The configuration is simple as

    <configSections>
    <sectionGroup name="elmah">
      <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
    </sectionGroup>
    </configSections>
    ...
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
    </httpModules>
    ...
    <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
    </modules>
    </system.webServer>
    <elmah>
    
    <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />
    
    </elmah>
    ...
    <system.webServer>
      <handlers>
        <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
      </handlers>
    </system.webServer>

    This will configure storage in /App_Data folder and you could browse errors via /elmah.axd URL. 

    There is much more you could configure, e.g. security, filtering of errors, etc. - you could find more information on ELMAH's website.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 12, 2015 6:20 AM
  • User264732274 posted

    thanks for answer but elmah related config statement are not very clear like what they will do and which is required and which is optional ?

    u said "It saves every error in single xml but you could sort files by date of creation or navigate through using its web interface." how could i configure elmah as a result every day it will generate a new xml file with specific format like Error_ddMMyyyy.xml for storing the error. guide me with sample code and configuration. can we use elmah in win application or is it specific to web application only?

    thanks

    Monday, April 13, 2015 3:06 PM
  • User1508394307 posted

    Did you read their documentation? I guess you didn't.

    I already provided you minimum required settings in the web.config. They have much more because ELMAH could mail you every error or store it in a database. As I said above it does not save errors in common file (at least I don't know such settings).

    The question is also why do you need to have one file? You will not be able to read it with notepad so your idea is useless. 

    Tuesday, April 14, 2015 3:10 AM
  • User264732274 posted

    from your explanation it seems that we can not store error info into xml or db by using elmah logger. if it is the fact then elmah is not for my purpose.

    Tuesday, April 14, 2015 7:01 AM
  • User1508394307 posted

    from your explanation it seems that we can not store error info into xml or db by using elmah logger.

    I think I told you the opposite, ELMAH stores errors in xml or database. 

    Tuesday, April 14, 2015 7:52 AM
  • User264732274 posted

    i developed the same long time ago. here i like to share my full code for logger saved log data in xml file date wise.

    logger class code

    using System.IO;
    using System.Xml;
    using System.Threading;
    
    public class BBALogger
        {
            public enum MsgType
            {
                Error ,
                Info 
            }
    
            public static BBALogger Instance
            {
                get
                {
                    if (_Instance == null)
                    {
                        lock (_SyncRoot)
                        {
                            if (_Instance == null)
                                _Instance = new BBALogger();
                        }
                    }
                    return _Instance;
                }
            }
    
            private static BBALogger _Instance;
            private static object _SyncRoot = new Object();
            private static ReaderWriterLockSlim _readWriteLock = new ReaderWriterLockSlim();
    
            private BBALogger()
            {
                LogFileName = DateTime.Now.ToString("dd-MM-yyyy");
                LogFileExtension = ".xml";
                LogPath= Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\Log";
            }
    
            public StreamWriter Writer { get; set; }
    
            public string LogPath { get; set; }
    
            public string LogFileName { get; set; }
    
            public string LogFileExtension { get; set; }
    
            public string LogFile { get { return LogFileName + LogFileExtension; } }
    
            public string LogFullPath { get { return Path.Combine(LogPath, LogFile); } }
    
            public bool LogExists { get { return File.Exists(LogFullPath); } }
    
            public void WriteToLog(String inLogMessage, MsgType msgtype)
            {
                _readWriteLock.EnterWriteLock();
                try
                {
                    LogFileName = DateTime.Now.ToString("dd-MM-yyyy");
    
                    if (!Directory.Exists(LogPath))
                    {
                        Directory.CreateDirectory(LogPath);
                    }
    
                    var settings = new System.Xml.XmlWriterSettings
                    {
                        OmitXmlDeclaration = true,
                        Indent = true
                    };
    
                    StringBuilder sbuilder = new StringBuilder();
                    using (StringWriter sw = new StringWriter(sbuilder))
                    {
                        using (XmlWriter w = XmlWriter.Create(sw, settings))
                        {
                            w.WriteStartElement("LogInfo");
                            w.WriteElementString("Time", DateTime.Now.ToString());
                            if (msgtype == MsgType.Error)
                                w.WriteElementString("Error", inLogMessage);
                            else if (msgtype == MsgType.Info)
                                w.WriteElementString("Info", inLogMessage);
    
                            w.WriteEndElement();
                        }
                    }
                    using (StreamWriter Writer = new StreamWriter(LogFullPath, true, Encoding.UTF8))
                    {
                        Writer.WriteLine(sbuilder.ToString());
                    }
                }
                catch (Exception ex)
                {
    
                }
                finally
                {
                    _readWriteLock.ExitWriteLock();
                }
            }
    
            public static void Write(String inLogMessage, MsgType msgtype)
            {
                Instance.WriteToLog(inLogMessage, msgtype);
            }
        }

    Calling or using this way

        BBALogger.Write("pp1", BBALogger.MsgType.Error);
        BBALogger.Write("pp2", BBALogger.MsgType.Error);
        BBALogger.Write("pp3", BBALogger.MsgType.Info);
        MessageBox.Show("done");
    Tuesday, April 14, 2015 2:14 PM