繁体   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