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 492643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T02:08:01+00:00 2026-05-13T02:08:01+00:00

I am trying to make a log viewer displaying events from different sources but

  • 0

I am trying to make a log viewer displaying events from different sources but ordered by a time stamp. I have a felling I can use C# Linq for this but how ?

example: I have one list of events read from files into a strig list sorted by a datetime stamp.

Another source of events are database inserts for which I use Linq to extract the same time segment as the first list. The database also have a time stamp.

What I want is to have list showing all events listed as they happen in real time. i.e a dabase insert may lead to an exception logged in a disk file a second later.

I guess I am looking for a way to join and sort these to lists sharing only one common field the timestamp, ending up with a collection I can use foreach over even though each element could be of different kind.

Any Ideas ?

Martin

  • 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-13T02:08:01+00:00Added an answer on May 13, 2026 at 2:08 am

    You can use Linq to transform both data sources to the same type, and then combine them and sort them. Here I have some objects from a pretend database table T_Log which has a Timestamp field and some other fields, and another source is some strings from a fake file where each string contains a timestamp at the start of the line. I transformed them both to a custom class CommonLog and then used this to sort. The CommonLog contains a reference to the original objects so if I need more detailed information I can cast and get that information.

    A more lightweight implementation could convert to a class that already exists, such as KeyValuePair<DateTime, object>.

    Here’s the code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    class Program
    {    
        // Fake database class.
        class T_Log
        {
            public DateTime Timestamp { get; set; }
            public string Info { get; set; }
            public int Priority { get; set; }
        }
    
        static void Main(string[] args)
        {
            // Create some events in the fake database.
            List<T_Log> dbLogs = new List<T_Log> {
                new T_Log { Timestamp = new DateTime(2009, 2, 5), Info = "db: foo", Priority = 1 },
                new T_Log { Timestamp = new DateTime(2009, 2, 9), Info = "db: bar", Priority = 2 }
            };
    
            // Create some lines in a fake file.
            List<string> fileLogs = new List<string> {
                "2009-02-06: File foo",
                "2009-02-10: File bar"
            };
    
    
            var logFromDb =
                dbLogs.Select(x => new CommonLog(
                              x.Timestamp,
                              string.Format("{1} [Priority={2}]",
                                            x.Timestamp,
                                            x.Info,
                                            x.Priority),
                              x));
    
            var logFromFile =
                fileLogs.Select(x => new CommonLog(
                                DateTime.Parse(x.Substring(0, x.IndexOf(':'))),
                                x.Substring(x.IndexOf(':') + 2),
                                x
                    ));
    
            var combinedLog = logFromDb.Concat(logFromFile).OrderBy(x => x.Timestamp);
            foreach (var logEntry in combinedLog)
                Console.WriteLine("{0}: {1}", logEntry.Timestamp, logEntry.Log);
        }
    }
    
    // This class is used to store logs from any source.
    class CommonLog
    {
        public CommonLog(DateTime timestamp,
                         string log,
                         object original)
        {
            this.Timestamp = timestamp;
            this.Log = log;
            this.Original = original;
        }
    
        public DateTime Timestamp { get; private set; }
        public string Log { get; private set; }
        public object Original { get; private set; }
    }
    

    Output:

    05-02-2009 00:00:00: db: foo [Priority=0]
    06-02-2009 00:00:00: file: baz
    09-02-2009 00:00:00: db: bar [Priority=0]
    10-02-2009 00:00:00: file: quux
    

    Update: Martin replied the following in a comment to this post, but it was hard to read due to lack of formatting in comments. Here it is with formatting:

    var ld = rs.Select(x => new KeyValuePair<DateTime, object>(DateTime.Parse(x[0]), x))
               .Concat(ta.Select(y => new KeyValuePair<DateTime, object>(y.Tidspunkt, y)))
               .OrderBy(d => d.Key); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to make wall post from multiple pages but when i
I am trying to make a log call direct different levels of output to
I'm trying to make a PHP script, I have the script finished but it
I am trying to make a simple script to automate and log synchronization via
I'm trying to make a log-in log-off with Ajax supported. I made some logic
I spent a day trying to make Ent Lib Logging work and log anything
I'm trying to make a website with a log on / log off feature
I am trying to make a simple AJAX calling in PHP and have a
I have been trying to make a list box report parameter with multiple choice
I am trying to make my app print something to log when screen is

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.