[펌]Log Parser COM API C#

요즘 한창 고민중인 로그 문제로 문서를 찾아 보다가 ㅠㅠ 발견한것…
출처 : http://www.wssplex.net/TipnTech.aspx?Seq=283


Log Parser COM API C#



www.wssplex.net


Log Parser 는 여러가지 로그 및 데이터를 파싱해서 특정 포맷으로 빠르게 변환해 주는 유틸로 잘 알려져 있다.

다운로드 : http://www.microsoft.com/downloads/details.aspx?FamilyID=890cd06b
-abf8-4c25-91b2-f8d975cf8c07&displaylang=en


Vbscript 나 Jscript 에서 COM 오브젝트를 호출하여 이용할수 있으며, 닷넷에서도 호출을 제공한다. 물론 RCW 로 래핑을 해줘야 하는데 이부분은 Visual Stuido 에서 참조추가를 하면 자동으로 처리를 해주므로 크게 신경쓰지 않아도 된다.

참조된 클래스를 보면, 소스포맷 및 출력포맷에 따른 각각의 클래스를 제공한다. 이유는 물론 데이터 포맷이 각각 다르므로 하나의 클래스에서 다 처리해준다는 것은 경우의 수가 많아서 일지도 모르겠다.


다음 예제는, Log Parser 에서 기본으로 제공하는 샘플코드 이다. 이러한 샘플코드를 소개하는 이유는 Log Parser 명령줄 유틸리티로는 서버관리자가 원하는 다양한 방식의 운영이 조금 불편한 점도 없지 않다.

이를 테면, 매일 웹사이트 로그중 웹오류인 500 에 대해서 파싱된 데이터를 특정 메일주소로 발송을 해준다던가 특정 디렉토리에 넣어주는 방법도 있을 것이다.



using System;
using LogQuery = Interop.MSUtil.LogQueryClassClass;
using EventLogInputFormat = Interop.MSUtil.COMEventLogInputContextClassClass;
using LogRecordSet = Interop.MSUtil.ILogRecordset;

class LogParserSample
{
    public static void Main(string[] Args)
    {
        try
        {
            // Instantiate the LogQuery object
            LogQuery oLogQuery = new LogQuery();

            // Instantiate the Event Log Input Format object
            EventLogInputFormat oEVTInputFormat = new EventLogInputFormat();

            // Set its “direction” parameter to “BW”
            oEVTInputFormat.direction = “BW”;

            // Create the query
            string query = @”SELECT TOP 50 SourceName, EventID, Message FROM System”;

            // Execute the query
            LogRecordSet oRecordSet = oLogQuery.Execute(query, oEVTInputFormat);

            // Browse the recordset
            for(; !oRecordSet.atEnd(); oRecordSet.moveNext())
            {
                Console.WriteLine(oRecordSet.getRecord().toNativeString(“,”));
            }

            // Close the recordset
            oRecordSet.close();
        }
        catch(System.Runtime.InteropServices.COMException exc)
        {
            Console.WriteLine(“Unexpected error: ” + exc.Message);
        }
    }
}



위 예제는 이벤트로그를 소스 데이터로 이용하는 경우인데., 맨위 클래스 목록에 있는 모든것이 이용이 가능하다.

IIS로그라면,


COMIISW3CInputContextClassClass iis = new COMIISW3CInputContextClassClass();

LogRecordSet oRecordSet = oLogQuery.Execute(query, iis);

메일 발송을 한다면, 메일 발송 처리 클래스 위 for 루프내에 StringBuilder 나 String 변수에 값을 저장한다음 그 것을 본문내용으로 해주거나,

System.IO 를 통해서 특정 디렉토리에 파일쓰기 방법도 있을수 있고, 또다른 방법이라면, COMSQLOutputContextClass 를 이용하여 SQL 서버의 특정 DB에 넣는 방법도 있을수 있다.

댓글 남기기