2016年10月12日 星期三

C#-將Log儲存成ArrayList且存成txt


將記錄變成ArrayList
之後再一併存至資料庫或txt....等



/// <summary>
/// 將log記錄到array,並選擇是否顯示在畫面上
/// </summary>
public class logArrayWrite
{
    ArrayList loglist; // 操作記錄
    bool showLog = false; // 是否顯示在畫面上
    public ArrayList logListMemo
    {
        set
        {
            loglist = value;
        }
        get
        {
            return loglist;
        }
    }
    public bool showLogInScreen
    {
        set
        {
            showLog = value;
        }
        get
        {
            return showLog;
        }
    }
    public void Add(string value)
    {
        if (loglist == null)
        {
            loglist = new ArrayList();
        }
        if (showLog)
        {
            Console.WriteLine(value);
        }
        loglist.Add(value);
    }
}


public static void setLog(ArrayList strWrite)
{
    AbstractEngine engine = null;
    try
    {
        engine = getEngine();
        // 儲存位置+檔名
        string url = saveFolder + "\\ㄅㄆㄇ" + DateTime.Now.ToString("yyyy/MM/dd").Replace("/", "") + ".log";
        StreamWriter writer = new StreamWriter(url, true, System.Text.Encoding.Default);
        foreach (string str in strWrite)
        {
            writer.WriteLine(str);
        }
        writer.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    finally
    {
        if (engine != null)
        {
            engine.close();
        }
    }
}