[Logging Block] Write more message at a time

Topics: Logging Application Block
Oct 27, 2011 at 8:40 PM

Hi there, I am pretty new to this library.

I want to know if is possible to write an IEnumerable<LogEntry> at the end of a procedure. Look at the following code:

    class Program
    {
        static void Main(string[] args)
        {
            LogWriter logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

            LogEntry entry = new LogEntry();
            entry.Message = "Start Logging";
            entry.Categories.Add("General");

            logger.Write(entry);

            for (int i = 0; i < 100; i++)
            {
                entry = new LogEntry();
                entry.Message = "--" + i.ToString();
                entry.Categories.Add("General");
                logger.Write(entry);
                for (int j = 0; i < 50; i++)
                {
                    entry = new LogEntry();
                    entry.Message = "----" + j.ToString();
                    entry.Categories.Add("General");
                    logger.Write(entry);
                }
            }

            entry = new LogEntry();
            entry.Message = "End Logging";
            entry.Categories.Add("General");

            logger.Write(entry);

        }
    }

I do not want to call the logger.IsLoggingEnabled() every time I call the logger.Write() method.
It is possible to do this?

Thanks
BadAng

Oct 27, 2011 at 11:18 PM

Hi BadAng,

Thanks for posting.  I'm not sure I understand your question.  Do you want to add LogEntry's to an IEnumerable<LogEntry> and then log them all at the end?  What is the specific problem you are trying to solve?

You don't have to call IsLoggingEnabled() -- the Write method will check that for you (along with any filters) and not log any entries that don't pass the checks.  IsLoggingEnabled() is useful when you want to avoid expensive operations when the result is not going to be logged.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com

Oct 28, 2011 at 7:52 AM

Hi rand, probably I need tracing instead of logging.

I am trying to write a log file to trace my application performance. I would write a text files at one single time, at the end. It will be more effective, isn't it?

Oct 29, 2011 at 4:19 AM

If you are looking to trace method entries and exits along with timings there is the Tracer class.

I don't think deferring the logging will gain you anything.  It might if you could somehow have a bulk write operation but that doesn't really fit with the Enterprise Library design.  Actually, that doesn't really fit with most logging frameworks that I'm aware of. 

Are you trying to optimize the tracing performance?

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com