log4net(またはその他のロギングフレームワーク)を使用する利点はありますか?ネイティブ.net でファイルに書き込むだけです。これまではユーティリティクラスを使用してファイルに書き込みました。そのようなフレームワークが 私のクラス(私のために正常に動作します)私のクラスを捨て、一部のロギングフレームワークに変換する点がありまし ので、基本的に私の質問を終了することを知らなかった.netネイティブ書き込みファイル対任意のログフレームワーク
class FileUtils
{
string mPath;
private static object locker = new Object();
public void createNewLogFile(string path)
{
try
{
lock (locker)
{
mPath = path;
string backUpPath = path + @".bak";
FileInfo MyFile = new FileInfo(path);
Directory.CreateDirectory(Path.GetDirectoryName(path));
if (File.Exists(path))
{
if (new FileInfo(path).Length > Math.Pow(2, 20))
{
if (File.Exists(backUpPath))
{
File.Delete(backUpPath);
}
File.Copy(path, backUpPath);
}
}
}
}
catch (Exception ex)
{
// outputFile.Close();
var t = ex;
throw;
}
}
public void log (string fileLocation ,bool newLine, params string[] input)
{
createNewLogFile(fileLocation);
using (StreamWriter outputFile = File.AppendText(mPath))
{
outputFile.AutoFlush = true;
try
{
if (newLine)
outputFile.Write(Environment.NewLine + DateTime.Now.ToString("dd-MM-yy HH:mm:ss "));
for (int i = 0; i < input.Length; i++)
{
outputFile.Write(input[i] + " ");
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
outputFile.Close();
}
}
}
}
私は恐れますが、これは意見に基づくものです。もちろん、ロギングフレームワーク**には多くのメリットがあります。ランタイム中でも高度に構成可能です。あなたは車輪を再発明する必要はありません。出力はファイルにバインドされていません(Windowsのイベントログ、電子メール、データベースなどにログすることができます)。ロットの処理方法ではなく、メインタスクに集中することができます。 – Fildor