簡體   English   中英

退出控制台應用程序C#並寫入日志

[英]exiting console application c# and writing to log

因此,如果參數檢查失敗,我將嘗試退出控制台應用程序,但是,我仍然希望它記錄到文件中。 只要所有參數都正確,日志記錄就可以正常工作。 但是,當參數檢查失敗並到達System.Environment.Exit(0)部分時,日志文件仍然完全為空。 到目前為止,這是代碼。 請幫忙,我已經盡力了。

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace I2C_File_Splitter
{
    class Program
    {
        static void Main(string[] args)
        {
            //get command line input paramaters  

            using (StreamWriter log = File.AppendText("Splitter_log.txt"))
            {


                log.WriteLine(DateTime.Now + " ******************************************** SPLITTER STARTED ****************************************************************");
                log.WriteLine(DateTime.Now + " FILE: " + args[0] + " DESTINATION: " + args[1] + " MAX COUNT PER FILE: " + args[2]);


                if (args.Length == 0)
                    System.Environment.Exit(0);


                string originalFile = args[0];

                string destination = args[1];
                int fileLength = Convert.ToInt32(args[2]);
                string fileName;
                string fileExtension;
                string line;
                int fileNumber = 1;




                if (!File.Exists(originalFile))
                {
                    log.WriteLine(DateTime.Now + " Error: Transfund file not found for: " + args[0]);
                    log.WriteLine(DateTime.Now + " ******************************************** SPLITTER ENDED ****************************************************************");
                    System.Environment.Exit(0);

                }


                if (!Directory.Exists(destination))
                {
                    log.WriteLine(DateTime.Now + " Error: destination directory not found for: " + args[1] );
                    log.WriteLine(DateTime.Now + " ******************************************** SPLITTER ENDED ****************************************************************");
                    System.Environment.Exit(0);
                }


                if (fileLength < 0)
                {
                    log.WriteLine(DateTime.Now + " Error: file length must be greater than 0. Incorrect value " + args[2]);
                    log.WriteLine(DateTime.Now + " ******************************************** SPLITTER ENDED ****************************************************************");
                    System.Environment.Exit(0);
                }


                //get file name and file extension
                fileName = Path.GetFileNameWithoutExtension(originalFile);
                fileExtension = Path.GetExtension(originalFile);



                StreamReader file = new StreamReader(originalFile);

                log.WriteLine(DateTime.Now + " processing: " + fileName); 

                string header = file.ReadLine(); //get first line

                while ((line = file.ReadLine()) != null)
                {
                    StreamWriter newFile = new StreamWriter(destination + "\\" + fileName + "_" + fileNumber.ToString() + fileExtension);
                    newFile.WriteLine(header);
                    newFile.WriteLine(line);

                    int counter = 1;
                    while (counter < fileLength)
                    {
                        line = file.ReadLine();

                        if (line == null)
                            break;

                        newFile.WriteLine(line);
                        counter++;

                    }
                    newFile.Close();
                    log.WriteLine(DateTime.Now + " " + fileName + "_" + fileNumber.ToString() + fileExtension + " created. Card count: " + counter);
                    fileNumber++;
                }

                log.WriteLine(DateTime.Now + " Processing completed: " + fileName);
                log.WriteLine(DateTime.Now + " ******************************************** SPLITTER ENDED ****************************************************************");
            }
        }
    }
}

調用Environment.Exit ,是在告訴它立即終止程序。

您的“日志”流永遠不會被刷新(當您到達using塊的末尾時會發生這種情況),因此沒有機會將其寫入文件。

嘗試在調用exit之前沖洗流。

if (!Directory.Exists(destination))
{
    log.WriteLine(DateTime.Now + " Error: destination directory not found for: " + args[1] );
    log.WriteLine(DateTime.Now + " ******************************************** SPLITTER ENDED ****************************************************************");

    // write all pending log messages to the file
    log.Flush();

    System.Environment.Exit(0);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM