在托管环境中日志文件文本格式更改中编写错误日志文本时

时间:2018-01-08 09:43:07

标签: c# .net iis

Localhost创建了这样的文件: - 模板ID |节点树|行号|列名|错误类型|错误说明

Localhost Downloaded File Look like This

Hosted Environment像这样创建了FIle:

模板ID |节点树|行号|列名|错误类型|错误 描述

in Hosted Environment Downloaded File Look like This

if(File.Exists(filename))             {

                using (StreamWriter sw = File.AppendText(filename))
                {

                    int nlength = -80;
                     string format = "{0,-20} | {1,"+nlength+"} | {2,-20} | {3,-50} | {4,-20} | {5,-20}";
                    if (new FileInfo(filename).Length == 0)
                    {
                        string Header = string.Empty;

                        Header = string.Format(format, "Column 1", ""Column 2", ""Column 3", "Column Name 4", ""Column 5", "Error Description" + line);


                    }

                    error = string.Format(format, TempteID, NodTree, Rober, ColName, prmType, prmMessage + line);


                    sw.WriteLine(error);
                    sw.Flush();
                    sw.Close();
                }

我在C#中使用Stream Writer。即在AppData文件夹的文本文件中写入SOme错误行。

当我在本地主机中运行此代码时,FOrmatting正在完美但在服务器格式化的托管应用程序未正确到来之后。

正如我在屏幕截图中提到的那样。

1 个答案:

答案 0 :(得分:1)

尝试如下代码。标签宽度将根据输出设备而变化。

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

namespace ConsoleApplication19
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            StreamWriter writer = new StreamWriter(FILENAME);


            List<List<string>> inputs = new List<List<string>>() {
                 new List<string>() {"Template ID", "Node Tree", "Row Number", "Column Name", "Error Type"," Error"},
                 new List<string>() {"Description 21843 ", "VOUCHER", "16428000","","Error", "Parent Name is not Correct"}
            };

            foreach (List<string> input in inputs)
            {
                writer.WriteLine(string.Join("\t|", input));
            }


            writer.Flush();
            writer.Close();
        }
    }
}