'2006/02/20'에 해당되는 글 2건

  1. 2006.02.20 [HowTo]NANT 사용하기
  2. 2006.02.20 [HowTo]EventLog 목록,내용 읽어오기(C#)
.NET General2006. 2. 20. 18:27

NAnt를 혹시 사용해보셨는지?

Ant는 원래 Java에서 사용하던 빌드 툴이다. C에서 사용하는 Make와 같은 역할을 하지만 역시 Java답게 운영체제나 시스템에 독립적인 빌드환경을 제공하기 위해서 Apache그룹에서 만든 빌드 프레임워크인데, NAnt는 SourceForge 커뮤니티에서 제작한 Ant의 .NET 버전이라고 생각하면 된다.

아래와 같은 빌드정의 파일(.build)을 XML포맷으로 만들고 그 파일이 있는 디렉토리에서 NAnt라고 Command-Line 명령을 실행하기만 하면 된다. 비주얼 스튜디오를 열지않고도 빌드를 할 수 있고, Nightly Build(Daily)도 가능하며, 서비스 형태로 몇 시간 혹은 몇 분마다 빌드를 하는 것도 가능하다. 그리고 컴파일뿐만 아니라 여러가지 부가적인 작업들도 실행을 시킬 수가 있다. 밑의 예제처럼 NUnit 테스트, FxCop을 이용한 코드 리뷰 등도 가능하다.


  1. <?xml version="1.0"?>
  2. <!- 프로젝트 이름 지정 -->
  3. <project name="AntTest" default="build">
  4. <target name="build">
  5.   <echo message="Building AntTest ...."/>
  6.   <tstamp property="build.date" pattern="yyyyMMdd" verbose="true" />
  7.   <echo message="{build.date}"/>
  8.    <!-- 프레임워크 버전을 지정 -->
  9.    <available type="FrameworkSDK" resource="net-1.1" property="net-1.1.frameworksdk.present" />
  10.    <!-- 빌드할 솔루션을 지정. 이 솔루션을 컴파일하게 된다 -->
  11.    <solution configuration="debug" solutionfile="AntTestSln\AntTestSln.sln">
  12.    <!-- 웹 프로젝트는 웹경로를 지정해야 한다-->
  13.    <webmap>
  14.    <map url="http://localhost/AntTestWeb/AntTestWeb.csproj" path="..\WEB\AntTestWeb.csproj" />
  15.    </webmap>
  16.    </solution>
  17.     <!-- NUnit 을 사용해서 유닛테스트를 수행한다. 물론 NUnit 테스트용으로 만든 어셈블리에 한해서 -->
  18.     <nunit2>
  19.     <formatter type="Xml" usefile="true" extension=".xml" outputdir="TEST\results" />
  20.     <test assemblyname="TEST\bin\debug\TEST.dll"/>
  21.     </nunit2>
  22.     <!-- FxCop 으로 코드리뷰를 수행한다 -->
  23.     <exec program="FxCopCmd" commandline="/file:COM\bin\Debug\com.dll /out:review\review.xml" />
  24.     <!-- NUnit Report를 사용해서 NUnit 유닛테스트 결과를 리포트에 기록한다 -->
  25.     <nunit2report out="TEST\results\NUnitReport(No-Frame).html" >
  26.     <fileset>
  27.         <includes name="TEST\results\TEST.dll-results.xml" />
  28.     </fileset>
  29.     <summaries>
  30.         <includes name="TEST\bin\debug\TEST.xml" />
  31.     </summaries>
  32.     </nunit2report>
  33.     <mail
  34.               from="buildmaster@project.com"
  35.               tolist="buildmaster@project.com"
  36.               subject="{build.date} 빌드가 완료되었습니다"
  37.               mailhost="mail.project.com">
  38.     </mail>
  39. </target>
  40. </project>
 
Posted by kkongchi
C# & VB.NET2006. 2. 20. 14:41

그냥 서버에 접속해서 이벤트로그를 보면 되지 않느냐라고 생각하실 수도 있지만...
단일한 관리용 웹페이지에서 이벤트로그, DB의 로그 등을 모두 보고 싶어 할 수도 있다.
아래 코드는 C#으로 작성한 ASP.NET 페이지로, 주어진 이름의 이벤트로그의 데이터를 읽어와서 그리드에 바인딩시키는 코드이다.

  1. private void Page_Load(object sender, System.EventArgs e){
  2.   this.DataGrid1.DataSource = this.GetData("Comm.SQL");
  3.   this.DataGrid1.DataBind();
  4. }
  5. private DataSet GetData(string eventLogName){
  6.   System.Diagnostics.EventLog oLog = null;
  7.   System.Diagnostics.EventLogEntryCollection oEntryCollection = null;
  8.   System.Data.DataSet dsEventLog = null;
  9.   System.Data.DataTable dtEventLog = null;
  10.   try{
  11.        dsEventLog = new DataSet("EventLogDataList");
  12.        dtEventLog = new DataTable(eventLogName);
  13.        dtEventLog.Columns.Add("Type");
  14.        dtEventLog.Columns.Add("Day");
  15.        dtEventLog.Columns.Add("Time");
  16.        dtEventLog.Columns.Add("Source");
  17.        dtEventLog.Columns.Add("Category");
  18.        dtEventLog.Columns.Add("Event");
  19.        dtEventLog.Columns.Add("User");
  20.        dtEventLog.Columns.Add("Machine");
  21.        oLog = new System.Diagnostics.EventLog(eventLogName);
  22.        oEntryCollection = oLog.Entries;
  23.        for(int i=0;i<oEntryCollection.Count;i++){
  24.            string[] arrLogEntry = new string[8];
  25.            arrLogEntry[0] = oEntryCollection[i].EntryType.ToString();
  26.            arrLogEntry[1] = oEntryCollection[i].TimeGenerated.ToShortDateString();
  27.            arrLogEntry[2] = oEntryCollection[i].TimeGenerated.ToShortTimeString();
  28.            arrLogEntry[2] = "<a href='webform2.aspx?LogName=" + eventLogName + "&Index=" + i.ToString() + "'>" + arrLogEntry[2] + "</a>";
  29.            arrLogEntry[3] = oEntryCollection[i].Source;
  30.            arrLogEntry[4] = oEntryCollection[i].Category;
  31.            arrLogEntry[5] = oEntryCollection[i].EventID.ToString();
  32.            arrLogEntry[6] = oEntryCollection[i].UserName;
  33.            arrLogEntry[7] = oEntryCollection[i].MachineName;
  34.             dtEventLog.Rows.Add(arrLogEntry);
  35.      }
  36.      dsEventLog.Tables.Add(dtEventLog);
  37.   }
  38.   catch(Exception ex)
  39.   {
  40.        throw ex;
  41.   }
  42.   return dsEventLog;
  43. }


위 코드는 이벤트뷰어와 똑같은 칼럼 순서로 이벤트 리스트를 그리드에 뿌리게 된다.
2번째 칼럼은 원래는 시간 데이터인데, 거기에 링크를 거는 부분이 있다.
이벤트 뷰어에서도 더블 클릭을 하면 이벤트에 대한 자세한 사항을 나타내는 창이 뜨는데,
아래는 바로 그 링크에서 연결되는 Detail 화면을 뿌리기 위한 코드이다.

  1. protected System.Web.UI.HtmlControls.HtmlInputText txtDay;
  2. protected System.Web.UI.HtmlControls.HtmlInputText txtTime;
  3. protected System.Web.UI.HtmlControls.HtmlInputText txtType;
  4. protected System.Web.UI.HtmlControls.HtmlInputText txtSource;
  5. protected System.Web.UI.HtmlControls.HtmlInputText txtCategory;
  6. protected System.Web.UI.HtmlControls.HtmlInputText txtEvent;
  7. protected System.Web.UI.HtmlControls.HtmlInputText txtUser;
  8. protected System.Web.UI.HtmlControls.HtmlInputText txtMachine;
  9. protected System.Web.UI.HtmlControls.HtmlTextArea txtDescription;
  10. private void Page_Load(object sender, System.EventArgs e)
  11. {
  12.   try
  13.   {
  14.     string strLogName = Request["LogName"];
  15.     string strIndex = Request["Index"];
  16.     int nIndex = Convert.ToInt32(strIndex);
  17.     this.SetContent(strLogName, nIndex);
  18.   }
  19.   catch(Exception ex)
  20.   {
  21.     Response.Write(ex.ToString());
  22.   }
  23. }
  24. private void SetContent(string logName, int index)
  25. {
  26.   System.Diagnostics.EventLog oLog = null;
  27.   System.Diagnostics.EventLogEntry oEntry = null;
  28.   try
  29.   {
  30.     oLog = new System.Diagnostics.EventLog(logName);
  31.     oEntry = oLog.Entries[index];
  32.     this.txtDay.Value = oEntry.TimeGenerated.ToShortDateString();
  33.     this.txtTime.Value = oEntry.TimeGenerated.ToLongTimeString();
  34.     this.txtType.Value = oEntry.EntryType.ToString();
  35.     this.txtSource.Value = oEntry.Source;
  36.     this.txtCategory.Value = oEntry.Category;
  37.     this.txtEvent.Value = oEntry.EventID.ToString();
  38.     this.txtUser.Value = oEntry.UserName;
  39.     this.txtMachine.Value = oEntry.MachineName;
  40.     this.txtDescription.Value = oEntry.Message;
  41.   }
  42.   catch(Exception ex)
  43.   {
  44.     throw ex;
  45.   }
  46. }
 
Posted by kkongchi