Log管理のライブラリーを書いてみた。

C#でバックグラウンド処理とかでログを吐かせるのがすごくだるかったので、ログ処理用のライブラリーを書いてみた。
他の言語で言う、print文みたいなことをやらせることができる。
一番の売りは、フォーム上に表示可能というところと、シングルトンなので、どこからも同じ呼び出しができるというところか。
結構便利だと思う。
あー、でも、ログファイルを書き込むってことはしていません。見るだけです。

使い方

下に書いた、Log.cs、LogTextBox.csのソースをコピーして、ソリューションに含める。
デザインでLogTextBoxを適当に配置してから。

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            MyLib.Log.AddControl(logTextBox1);
        }
        public void LooP()
        {
            for(int i=0;i<1000;i++)
            {
                MyLib.Log.LogWriteLine(i.ToString());
              System.Threading.Thread.Sleep(100);
            }
        }

    }

とやると、LogTextBoxにどんどん書き込まれていく。バックグランド処理でも可。

一応、

MyLib.Log.GetInstance().CanWrite = false 

とすると、書き込みが無効になる。細かいことはソース読めばわかるでしょ。

log.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MyLib
{
    public class Log
    {
        Dictionary<string, MyLib.Controls.LogTextBox> logTextBoxControlDic = new Dictionary<string, MyLib.Controls.LogTextBox>();
        StringBuilder logBuilder = new StringBuilder();
        LogType logType = LogType.Console;
        bool canWrite = true;

        /// <summary>
        /// ログの書き込みができる。
        /// </summary>
        public bool CanWrite
        {
            get { return canWrite; }
            set { canWrite = value; }
        }

        private Log()
        {
            instance = this;
        }
        static Log instance;

        private Log(LogType type)
        {
            logType = type;
            instance = this;
        }

        public static Log GetInstance()
        {
            if (instance == null)
            {
                new Log();
            }
            return instance;
        }

        public LogType LogType
        {
            get { return logType; }
            set { logType = value; }
        }


        public void AddControl_(MyLib.Controls.LogTextBox logbox)
        {
            logTextBoxControlDic.Add(logbox.Name,logbox);
            logType = LogType.LogTextBox;
        }
        public static void AddControl(MyLib.Controls.LogTextBox logbox)
        {
            GetInstance().AddControl_(logbox);
        }

        public void ClearControl_()
        {
            logTextBoxControlDic.Clear();
        }
        public static void ClearControl()
        {
            GetInstance().ClearControl_();
        }


        public void LogClear()
        {
            logBuilder.Length = 0;
            foreach (MyLib.Controls.LogTextBox textBox in logTextBoxControlDic.Values)
            {
                textBox.LogClear();
            }

        }
        public void LogWrite_(string str, LogType type, string name)
        {
            if (canWrite == false)
            {
                return;
            }
            if (type == LogType.Console)
            {
                System.Console.Write(str);
            }
            else if (type == LogType.Debag)
            {
                System.Diagnostics.Debug.Write(str);
            }
            else if (type == LogType.LogTextBox)
            {
                if (name != null && name.Length > 0)
                {
                    if (logTextBoxControlDic.ContainsKey(name))
                    {
                        logTextBoxControlDic[name].LogWrite(str);
                    }
                }
                else
                {
                    foreach (MyLib.Controls.LogTextBox textBox in logTextBoxControlDic.Values)
                    {
                        textBox.LogWrite(str);
                    }
                }
            }
            logBuilder.Append(str);

        }
        public void LogWrite_(string str, LogType type)
        {
            LogWrite(str, type);
        }

        public void LogWrite_(string str)
        {
            LogWrite(str, logType, null);
        }


        public static void LogWrite(string str, LogType type, string name)
        {
            GetInstance().LogWrite_(str, type, name);
        }

        public static void LogWrite(string str, LogType type)
        {
            GetInstance().LogWrite_(str, type);
        }

        public static void LogWrite(string str)
        {
            GetInstance().LogWrite_(str);
        }

        public void LogWriteLine_(string str, LogType type,string name)
        {
            LogWrite(str + Environment.NewLine, type,name);
        }

        public void LogWriteLine_(string str,LogType type)
        {
            LogWrite(str + Environment.NewLine,type);
        }

        public void LogWriteLine_(string str)
        {
            LogWrite(str + Environment.NewLine);
        }

        public void LogWriteLine_()
        {
            LogWrite(Environment.NewLine);
        }

        public void LogWriteLine_(LogType type)
        {
            LogWrite(Environment.NewLine,type);
        }

        public void LogWriteLine_(LogType type,string name)
        {
            LogWrite(Environment.NewLine, type,name);
        }

        public static void LogWriteLine()
        {
            GetInstance().LogWriteLine_();
        }

        public static void LogWriteLine(string str)
        {
            GetInstance().LogWriteLine_(str);
        }

        public static void LogWriteLine(string str,LogType type)
        {
            GetInstance().LogWriteLine_(str,type);
        }

        public static void LogWriteLine(string str, LogType type,string name)
        {
            GetInstance().LogWriteLine_(str, type,name);
        }


        public void AlertWrite_(string str, LogType type, string name)
        {
            if (canWrite == false)
            {
                return;
            }
            if (type == LogType.LogTextBox)
            {
                if (name != null && name.Length > 0)
                {
                    if (logTextBoxControlDic.ContainsKey(name))
                    {
                        logTextBoxControlDic[name].LogWrite(str);
                    }
                }
                else
                {
                    foreach (MyLib.Controls.LogTextBox textBox in logTextBoxControlDic.Values)
                    {
                        textBox.LogWrite(str);
                    }
                }
            }
            else
            {
                string str1 = "[警告]" + str;
                if (type == LogType.Console)
                {
                    System.Console.Write(str1);
                }
                else if (type == LogType.Debag)
                {
                    System.Diagnostics.Debug.Write(str1);
                }
            }
            string str2 = "[警告]" + str;
            logBuilder.Append(str2);
        }


        public void AlertWrite_(string str)
        {
            AlertWrite_(str, logType, null);
        }
        public void AlertWrite_(string str, LogType type)
        {
            AlertWrite_(str, type, null);
        }
        public void AlertWriteLine_()
        {
            AlertWrite_(Environment.NewLine);
        }

        public void AlertWriteLine_(string str)
        {
            AlertWrite_(str + Environment.NewLine);
        }

        public void AlertWriteLine_(string str, LogType type)
        {
            AlertWrite_(str + Environment.NewLine, type);
        }

        public void AlertWriteLine_(string str, LogType type, string name)
        {
            AlertWrite_(str + Environment.NewLine,type,name);
        }

        public static void AlertWrite(string str)
        {
            GetInstance().AlertWrite_(str);
        }

        public static void AlertWrite(string str, LogType type)
        {
            GetInstance().AlertWrite_(str, type);
        }
        public static void AlertWrite(string str, LogType type,string name)
        {
            GetInstance().AlertWrite_(str, type,name);
        }

        public static void AlertWriteLine(string str)
        {
            GetInstance().AlertWriteLine_(str);
        }

        public static void AlertWriteLine(string str, LogType type)
        {
            GetInstance().AlertWriteLine_(str, type);
        }

        public static void AlertWriteLine(string str, LogType type, string name)
        {
            GetInstance().AlertWriteLine_(str, type, name);
        }
       
    }
    public enum LogType
    {
        Console,
        Debag,
        LogTextBox
    }
}

冗長性がアホみたい。一応、記述を短くするためにシングルトン+静的関数を使用。おかげでどうでもいい処理が多い。

LogTextBox.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;

using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace MyLib.Controls
{
    public partial class LogTextBox : System.Windows.Forms.RichTextBox
    {
        public LogTextBox()
        {
            InitializeComponent();
          
        }

        public LogTextBox(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        delegate void LogWriteDelegate(string str, Color c);
        delegate void LogClearDelegate();

        public void LogWrite(string str, Color color)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new LogWriteDelegate(LogWrite), new object[] { str, color });
                return;
            }

            if (this.MaxLength * 0.99 < this.TextLength)
            {
                this.Clear();
            }

            this.SelectionColor = color;
            this.AppendText(str);

            this.Focus();
            this.Refresh();
        }

        /// <summary>
        /// 赤い文字で書きます。
        /// </summary>
        /// <param name="str"></param>
        public void AlertWrite(string str)
        {
            LogWrite(str, Color.Red);
        }

        /// <summary>
        /// 赤い文字で書きます。
        /// </summary>
        /// <param name="str"></param>
        public void AlertWriteLine(string str)
        {
            LogWrite(str + Environment.NewLine, Color.Red);
        }


        /// <summary>
        /// 黒い文字で普通に書きます。
        /// </summary>
        /// <param name="str"></param>
        public void LogWrite(string str)
        {
            LogWrite(str, Color.Black);
        }
        /// <summary>
        /// 黒い文字で普通に書きます。
        /// </summary>
        /// <param name="str"></param>
        public void LogWriteLine(string str)
        {
            LogWrite(str + Environment.NewLine, Color.Black);
        }

        public void LogClear()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new LogClearDelegate(LogClear));
                return;
            }
            this.Text = "";
            this.Invalidate();
        }
    }
}

どこかからの見よう見まねで作った。