none
ocurred a exception by Console.WriteLine RRS feed

  • Question

  • Dear Eveyone.

    i need to show a log into console.

    Exception has ben occured from Console.WriteLine.

    The message is " must complete convert() operation or call encoder.reset() before calling getbytes() or getbytecount(). encoder..."

    i have used VS2008, VS2010

    How can i solve this problem?

    lightdoll
    Sunday, March 7, 2010 3:56 PM

Answers

All replies

  • Hi, lightdoll:
        Could you provide some more details? Some code snippets?
    Please mark the right answer at right time.
    Thanks,
    Sam
    Monday, March 8, 2010 2:33 AM
  • Dear SamAgain

    i have written some code below

    before i call WriteToDisplay , i create log string of StringBuilder, then pass it to WriteToDisplay.


    void WriteToDisplay(string log)// log parameter will be log string.
    {
       // File Open

       try
       {
               Console.WriteLine(log); // Exception come frome here
               File.Write(log);
       }
       catch(Exception ex)
       {
          Console.WriteLine(log);
       }
    }



    lightdoll
    Monday, March 8, 2010 3:22 AM
  • Hi, lightdoll:
         The code you provided seems fine to me. Could you show some more details? How do you call this method? What args do you pass to it? How are the args constructed?


    Please mark the right answer at right time.
    Thanks,
    Sam
    Monday, March 8, 2010 4:11 AM
  • Dear SamAgain

      yes the code is right, but when i excute my program over 6 hours to display log into console windows.

    Suddenly, stop to display log into console window, so i debug my code, and see exception message , i got the message.

    i have attaced the log class below,

    i just called the function like LogProcess.WriteToDisplay("log data");

    i should solve the problem,

        public class LogProcess
        {
            private  int _lineCount = 0;
            private  TextWriter _logFw = null;
    
            private  string _logFileName = string.Empty;
            private string _defaultDirectory = string.Empty;
            private  object _lockObject = new object();
            private  Queue<string> _logFileList = new Queue<string>();
            static private  int _logFileLine = 0;
            private  Thread _trMerge = null;
    
    
            public void Initialize()
            {
                _lineCount = 0;
    
                if (_logFw != null)
                {
                    _logFw.Close();
                    _logFw.Dispose();
                    _logFw = null;
                }
    
                _logFileName = string.Empty;
            }
    
            static public void SetLogFileLine(int logFileLine)
            {
                LogProcess._logFileLine = logFileLine;
            }
    
    
            public void SetDefaultDirector(string defaultDirectory)
            {
                _defaultDirectory = defaultDirectory;
            }
    
            public void StopMergeTh()
            {
                if (_trMerge != null)
                {
                    _trMerge.Abort();
                    _trMerge.Join();
                    _trMerge = null;
                }
    
                _lineCount = 0;
            }
    
             public void WriteToFile(string log)
            {
                if (!Directory.Exists(_defaultDirectory))
                    Directory.CreateDirectory(_defaultDirectory);
                if (_lineCount == 0)
                {
                    CreateLogFile();
                    //ThreadStart ts = new ThreadStart(LogFileMergeTh);
                    //_trMerge = new Thread(ts);
                    //_trMerge.Start();
                }
                else if (_lineCount == LogProcess._logFileLine)
                {
                    _lineCount = 0;
                    _logFw.Close();
                    _logFw.Dispose();
                    _logFw = null;
                    CreateLogFile();
                }
    
    
                try
                {
                    _logFw.WriteLine(log);
                    _logFw.Flush();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                _lineCount++;
            }
    
             public void DisplayNotLine(string log)
            {
                Console.Write(log);
            }
    
             public void WriteAndDisplay(string log)
            {
                if (!Directory.Exists(_defaultDirectory))
                        Directory.CreateDirectory(_defaultDirectory);
                    if (_lineCount == 0)
                    {
                        CreateLogFile();
                        //ThreadStart ts = new ThreadStart(LogFileMergeTh);
                        //_trMerge = new Thread(ts);
                        //_trMerge.Start();
                    }
                    else if (_lineCount == LogProcess._logFileLine)
                    {
                        _lineCount = 0;
                        _logFw.Close();
                        _logFw.Dispose();
                        _logFw = null;
                        CreateLogFile();
                    }
    
    
                    try
                    {
                        Console.WriteLine(log);
                        _logFw.WriteLine(log);
                        _logFw.Flush();
                    }
                    catch (ArgumentException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    catch (Exception ex)
                    {
    
                        Console.WriteLine(ex.Message);
                    }
                    _lineCount++;
                
            }
    
            public void CreateLogFile()
            {
                DateTime createdTime = DateTime.Now;
                DateTimeFormatInfo dateTimeForamteInfoe = new DateTimeFormatInfo();
                dateTimeForamteInfoe.TimeSeparator = " ";
                _logFileName = System.IO.Path.Combine(_defaultDirectory, string.Format("{0:yyyy/M/d HHmmss}", createdTime)) + ".log";
                _logFw = new StreamWriter(_logFileName, true);
               
             //   _logFileList.Enqueue(_logFileName);
            }
    
             public void LogFileMergeTh()
            {
                string logFile = string.Empty;
                bool fFile = true;
                string mergeFile = string.Empty;
    
                while (true)
                {
                    if (_logFileList.Count > 3)
                    {
                        lock (_lockObject)
                        {
                            logFile = (string)_logFileList.Dequeue();
                        }
                        if (fFile == true)
                        {
                            string[] spPath = logFile.Split('.');
                            string[] spFileName = spPath[1].Split('\\');
                            string fileName = spFileName[spFileName.Length - 1];
                            mergeFile = Path.Combine(_defaultDirectory, fileName + ".mge");
                            fFile = false;
                        }
                        FileStream outFile = new FileStream(mergeFile, FileMode.OpenOrCreate, FileAccess.Write);
    
                        int data = 0;
                        byte[] buffer = new byte[1024];
                        for (int i = 0; i < 2; i++)
                        {
                            FileStream inFile = new FileStream(logFile, FileMode.OpenOrCreate, FileAccess.Read);
                            while ((data = inFile.Read(buffer, 0, 1024)) > 0)
                            {
                                outFile.Write(buffer, 0, data);
                            }
                            inFile.Close();
                            inFile.Dispose();
                            lock (_lockObject)
                            {
                                logFile = (string)_logFileList.Dequeue();
                            }
                            Thread.Sleep(TimeSpan.FromMilliseconds(0.5));
                        }
                        outFile.Close();
                        outFile.Dispose();
                        fFile = true;
                    }
                    Thread.Sleep(TimeSpan.FromMilliseconds(0.5));
                }
    
            }
        }


     

     


    lightdoll
    Monday, March 8, 2010 9:15 AM
  • Hi, lightdoll:
         As you originally stated, the exception message is related to getbytes() or convert(), but I didn't see their usage in your code. Still, I tried out your code after some small modifications as below, 


    public void CreateLogFile()
    {
        DateTimeFormatInfo dateTimeForamteInfoe = new DateTimeFormatInfo();
        dateTimeForamteInfoe.TimeSeparator = " ";
        DateTime createdTime = DateTime.Now;
        string file_name = string.Format("{0:yyyy_M_d HHmmss}", createdTime);//Here
        _logFileName = System.IO.Path.Combine(_defaultDirectory, file_name) + ".log";
        FileStream fs = File.Open(_logFileName, FileMode.OpenOrCreate);//Here
        fs.Close();
        _logFw = new StreamWriter(_logFileName, true);
    
        //   _logFileList.Enqueue(_logFileName);
    }
    


        These modifications are not intended to solve your problem but just for my test run. And my test code is like this:


    static void Main(string[] args)
    {
        LogProcess logger = new LogProcess();
        logger.Initialize();
        logger.SetDefaultDirectory(@"d:\test\");
        LogProcess.SetLogFileLine(3);
        while (true)
        {
            logger.WriteAndDisplay(DateTime.Now.ToString());
        }
    }
    


        I ran it for some while and things seem to be fine. Is there something interesting missing?
    Please mark the right answer at right time.
    Thanks,
    Sam
    Tuesday, March 9, 2010 8:37 AM
  • Dear Sam.

    i have found some solution from web, so i am testing modifid my code .

    yes i have never used the getbytes() and convert().
    getbytes() or convert() occrued inside Console.WriteLine.

    i think you can't find the exception from your modified code, because too small string.

    could you check this site http://www.886s.com/blog/?p=10,

    i am testing until now. it is fine.. i wil be running my program until tomorrow.


    but it's strange about Console.WriteLine in C#. i think MS should fix the Problem.

    thank you very much 

    lightdoll
    Tuesday, March 9, 2010 12:56 PM
  • Hi, lightdoll:

         I have found some similar cases to yours, such as:

        1.http://social.msdn.microsoft.com/Forums/zh-TW/233/thread/0eec91d5-ae19-48a0-bfbb-8462ccce6577

        2. http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/924c74ab-7239-4e17-a178-8db158a1d942
       
        As for now, you may seperate the logs into multiple batches instead of a single big one. This could be a workaround. Also, you may contact the https://connect.microsoft.com.
       
        Sorry for your inconvenience.


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by lightdoll Tuesday, March 16, 2010 2:31 AM
    Monday, March 15, 2010 3:45 AM
  • Dear Sam

    Thanks for your reply.

    Have a good day.
    lightdoll
    Tuesday, March 16, 2010 2:34 AM