C++Builder XEでCodeSite ExpressのFile Logging機能を使うチュートリアル。
前回の記事「C++Builder XEでCodeSite Expressを使用する(1)Live Logging チュートリアル」の続きです。
今回はログをファイルに出力します。
Form1のコンストラクタに次のコードを追加します。
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TCodeSiteDestination* dest = new TCodeSiteDestination( this );
dest->LogFile->Active = true;
dest->LogFile->FileName = "MyFirstLog.csl";
dest->LogFile->FilePath = "$(MyDocs)\\Logs";
CodeSite->Destination = dest;
}
FilePathに記述している「$(MyDocs)」は「マイドキュメント」のパスに置き換えられます。
プログラムを実行し、ボタン(Button1)を押します。
マイドキュメントのLogsフォルダーにMyFirstLog.cslファイルが生成されます。
MyFirstLog.cslを開くと、CodeSite File Viewreが起動します。
「Inspector」ボタンを押すと、オブジェクトの詳細を見ることができます。
Form1のコンストラクタに次のコードを追加します。
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TCodeSiteDestination* dest = new TCodeSiteDestination( this );
dest->LogFile->Active = true;
dest->LogFile->FileName = "MyFirstLog.csl";
dest->LogFile->FilePath = "$(MyDocs)\\Logs";
dest->Viewer->Active = true; //<=追加
CodeSite->Destination = dest;
}
プログラムを実行し、ボタン(Button1)を押します。
CodeSite Live Viewerが起動し、ログが出力されます。
ファイルにもログが出力されます。
Form1のコンストラクタに次のコードを追加します。
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TCodeSiteDestination* dest = new TCodeSiteDestination( this );
dest->LogFile->Active = true;
dest->LogFile->FileName = "MyFirstLog.csl";
dest->LogFile->FilePath = "$(MyDocs)\\Logs";
dest->LogFile->MaxParts = 3; //<= 追加
dest->LogFile->MaxSize = 200; //<= 追加
dest->Viewer->Active = true;
CodeSite->Destination = dest;
}
MaxSizeはログファイルの最大サイズをKbで指定します。
指定したファイルサイズを超えると、新しいファイルに出力します。
MaxPartsはログファイルの最大数を指定します。
プログラムを実行し、ボタン(Button1)を押します。
ボタン(Button1)を何回か押すと、ログファイルが作成されていきます。