Log Assembly for C#

Markus Freitag 3,786 Reputation points
2020-07-09T09:41:58.343+00:00

Hello,
I need a tool that creates log files for me without blocking the application should be done in parallel.

Furthermore I need to be able to store the log file in this format. <NameOfApplication><YYYY-MM-dd>.log

If the size exceeds 50MByte a new file should be written. After 15 days, they should be deleted automatically.

Are there FreeTools for these requirements? Does anyone have a good sample code for C#, WPF, VS2017? Thanks in advance.
Regards Markus

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,705 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,306 Reputation points
    2020-07-13T09:00:06.68+00:00

    Hi, try following Logger. Unfortunately, it is impossible for me to insert code into this buggy forum UI.

    11865-x.png


6 additional answers

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2020-07-10T03:42:28.943+00:00

    You can use Task Scheduler to delete the file automatically.
    First you need to create a simple project which use to delete the files only include the below method, build the project to get the exe.

     public static void DelectDir(string srcPath)  
            {  
                try  
                {  
                    DirectoryInfo dir = new DirectoryInfo(srcPath);  
                    FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  
                    foreach (FileSystemInfo i in fileinfo)  
                    {  
                        TimeSpan timeSpan = System.DateTime.Now- i.LastWriteTime;  
                        if (timeSpan.TotalSeconds>15*24*3600 )     //delete the files before 15 days       
                        {  
                            File.Delete(i.FullName);  
                        }  
                    }  
                }  
                catch (Exception e)  
                {  
                    throw;  
                }  
            }  
    

    Then you need to create a task Scheduler to run this exe. The step is :

    1. Search for "Task Scheduler" .
    2. Right-click on the "Task Scheduler Library" and click on the "New Folder" option.
    3. Enter the name of the new folder and click on the "OK" button.
    4. Task Scheduler Library > New Folder (Testing), then click on "Create Basic Task".
    5. Provide the Task Name.
    6. Choose when would you like your task to start and click next.
    7. Set the Start date and time of the task and click Next.
    8. Select "Start a program" option from a various list of actions and then click next.
    9. Click on Browse Button and choose "XX.exe" from your project file and finish the task.
    1 person found this answer helpful.

  2. Ken Tucker 5,846 Reputation points
    2020-07-10T19:45:57.7+00:00

    You also use a logging framework like serilog which lets you set the max number of days to keep a file and set max file size

    https://www.techrepository.in/blog/posts/rollover-log-files-automatically-in-an-asp-net-core-web-application-using-serilog

    1 person found this answer helpful.
    0 comments No comments

  3. Peter Fleischer (former MVP) 19,306 Reputation points
    2020-07-09T14:32:14.123+00:00

    Hi Markus, I use a simple Logger like this:
    11781-x.png


  4. Markus Freitag 3,786 Reputation points
    2020-07-10T13:53:00.59+00:00

    Hi Peter,
    App run continuously!

    App start only once per day or week. The best one time for month without error.

    Yes only if we restart the application we shoukd check the days, for example 14 (Configuration value)

    I have to be able to open and view the log file at all times, e.g. with Notepad++.

    Checking whether more than 50 MB is enough for one check per hour.

    Regards Markus and Thanks in advance.

    0 comments No comments