Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7545873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:02:51+00:00 2026-05-30T09:02:51+00:00

I wanted to ask for some logging mechanism or framework with specific function. I

  • 0

I wanted to ask for some logging mechanism or framework with specific function. I am already loggin in my app (dll library) with

Log.WriteLine("{0}.{1}()", System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType, System.Reflection.MethodInfo.GetCurrentMethod().Name);

where the Log is static class with capability to write to file using Streamwriter

public void LogWriteLine(string text, params object[] args) {
  lock (this) {
      StreamWriter log = new StreamWriter(logFile, true);
      if (log != null) {
        log.WriteLine("{0:yyyy-MM-dd HH:mm:ss} {1}", DateTime.Now, String.Format(text, args));
        log.Flush();
        log.Close();
      }
  }
}

My problem is, I don’t have the Log.WriteLine call all over my app, just in specific parts of apps, because it would create really big file. But now, I build my app, and posted it to the developers, they worked on it for some days. After that they send me bugs, but in my version the bugs are not anymore (the development continues on the app, so they could be fixed).

So I wanted to have in the app some setting file, to tell the app that I need more logs and be able to run the testers version of app without to rebuild it just with different settings for logger.

In short: how can I tell the app form some settings file, to log something specific only, for example just one class, or one method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:02:53+00:00Added an answer on May 30, 2026 at 9:02 am

    I have needed to do a similar sort of thing. This is how I did it.

    I created a category class and use that as a parameter when the logging object is initialised.

    /// <summary>
    /// Category object for logging
    /// </summary>
    public class Category
    {
        #region Private Members
        private bool    m_active;
        private string  m_name;
        private bool    m_excludeFromLogFile = false;
        #endregion
    
        /// <summary>
        /// Create a category and add it to the Logging category list
        /// </summary>
        /// <param name="name">The Name of the category</param>
        /// <param name="active">The active state of the category</param>
        /// <param name="exclude">If true any messages for this category will not be written to the log file</param>
        /// <param name="addedToList">If true then the new category will be added to the logging category list</param>
        public Category(string name, bool active, bool exclude, bool addedToList)
        {
            m_name = name;
            m_active = active;
            m_excludeFromLogFile = exclude;
    
            if(addedToList)
            {
                Log.GetInstance().AddCategory(this);
            }
        }
    
        #region Public Accessor Methods
            // .. Add accessors as required 
        #endregion
    }
    

    As you can see by the line “Log.GetInstance().AddCategory(this);”, my logging object is a singleton.

    The singleton, has some methods to add and remove categories

    /// <summary>
    /// Add a new category to the list of available categories
    /// </summary>
    /// <param name="newCat">The category object to add</param>
    public void AddCategory( Category newCat )
    {
        // Ensure that the category doesn't already exist in the list
        if( this.m_CategoryList.Contains( newCat ) == false )
        {
            // Add the new category to the list
            this.m_CategoryList.Add( newCat );
        }
    }
    
    /// <summary>
    /// Remove a category to the list of available categories
    /// </summary>
    /// <param name="catName">The name of the category to be removed</param>
    public void RemoveCategory( string catName )
    {
        Category toRemove = null;
    
        // Iterate through the categories looking for a match
        foreach( Category cat in this.m_CategoryList)
        {
            // Compare the category names (case insensitive)
            if( cat.Name.ToUpper() == catName.ToUpper() )
            {
                // Assign the category to remove to a local variable and exit the loop
                toRemove = cat;
                break;
            }
        }
    
        // Remove the category if it's been located
        if( toRemove != null )
        {
            this.m_CategoryList.Remove( toRemove );
        }
    }
    

    When processing the log event it is now just a case of checking the active state of the category to see if that message is required.

    /// <summary>
    /// Create a log entry in the log file and then Fire an event for the log message to be handled
    /// </summary>
    /// <param name="category">The category to log the message against</param>
    /// <param name="args"> Message logging arguments used by the event</param>
    public void WriteLine(Category category, MessageEventArgs args)
    {
        // Ensure that the category specified exists in the array list
        if( this.m_CategoryList.Contains( category ) )
        {
            // Ensure the category is active 
            if(category.Active == true)
            {
                if(!category.ExcludeFromLogFile)
                {
                    // Try and log the message to the log file
                    this.WriteLineToFile( category, args );
                }
    
                // Ensure an event handler has been assigned
                if(MessageEvent != null)
                {
                    // This message event is handled by the UI thread for updating screen components.
                    MessageEvent(category, args);
                }
            }
        }
    }
    

    Finally, if you want the message to be displayed on a screen, you’ll need to handle the message event in the UI thread. Here is a sample from one of my list view components…

    private void ListViewLogging_MessageEvent(Category category, MessageEventArgs args)
    {
        // Ensure the event was received in the UI thread
        if(this.InvokeRequired)
        {
            if(args.Message != null)
            {
                // We aren't in the UI thread so reFire the event using the main thread
                this.BeginInvoke(new MessageReceivedDelegate(this.ListViewLogging_MessageEvent), new object[]{category,args});
            }
        }
        else
        {
            // We are currently in the main thread.
            // Lock so no other thread can be handled until event processing has been finished
            lock(this)
            {
                // Create a new ListView item for the new message 
                ListViewItem newEntry = null;;
    
                // Determine the category type
                switch( category.Name )
                {
                    case "Serious":
                    {
                        // Serious error detected
                        if( args.Message.Length > 0 )
                        {
                            newEntry = new ListViewItem( new string[]{category.Name, args.Occurred.ToLongTimeString(), args.Message} );
                            newEntry.BackColor = Color.Red;
                        }
                        break;
                    }
                    case "Warning":
                    {
                        // Warning detected.
                        if( args.Message.Length > 0 )
                        {
                            newEntry = new ListViewItem( new string[]{category.Name, args.Occurred.ToLongTimeString(), args.Message} );
                            newEntry.BackColor = Color.Orange;
                        }
                        break;
                    }
                    case "Progress":
                    {
                        // If a message has been specified, log it
                        if( args.Message.Length > 0 )
                        {
                            newEntry = new ListViewItem( new string[]{"", args.Occurred.ToLongTimeString(), args.Message} );
                        }
                        break;
                    }
                    case "Debug":
                    {
                        // Just a standard Debug event so just display the text on the screen
                        if( args.Message.Length > 0 )
                        {
                            newEntry = new ListViewItem( new string[]{category.Name, args.Occurred.ToLongTimeString(), args.Message} );
                            newEntry.BackColor = Color.LightGreen;
                        }
                        break;
                    }
                    case "Info":
                    default:
                    {
                        // Just a standard event so just display the text on the screen
                        if( args.Message.Length > 0 )
                        {
                            newEntry = new ListViewItem( new string[]{category.Name, args.Occurred.ToLongTimeString(), args.Message} );
                        }
                        break;
                    }
                }
                // Add the item if it's been populated
                if( newEntry != null )
                {
                    this.Items.Add( newEntry );
                    this.EnsureVisible( this.Items.Count-1 );
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are having some performance issues with a page and I wanted to ask
Update2: What I really wanted to ask was already argued in a different page.
I don't do very much jquery / javascript but wanted to ask for some
I wanted to ask about using selector from a variable first I have: function
I'm creating a overview of TopDesk. I've wanted to ask some of you what
Hey guys I wanted to ask if you can do some conditional checks on
I have done some testing but I wanted to ask if anyone sees a
just wanted to ask is there a possibility to bind some listener to the
just wanted to ask is there a possibility to bind some listener to the
I wanted to ask, I have an array and I want to eliminate some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.