질문에 따라 요청과 관련된 모든 작업을 기록해야합니다. 나는 내 견해를 제공 할 것이고, 그것이 유용 할 것이기를 희망한다.
기존 프레임 워크를 사용할지 여부는 여러 가지 이유로 인해 달라 지므로 지금은 초점을 맞춤 구현에 대해 설명합니다.
먼저 로그 구조를 필요로이 문제를 달성하기 위해 :
using System;
public enum LogEntryType
{
Event,
Message,
Warning,
Error
}
public class LogEntry
{
public int? LogEntryID { get; set; }
public int? LogEntryType { get; set; }
public DateTime? EntryDate { get; set; }
public TimeSpan? ElapsedTime { get; set; }
public string Key { get; set; }
public string Description { get; set; }
}
다음, 당신은 예를 들어, 로거 객체를 생성 및 로깅 할 각 지점에 호출해야합니다
namespace MvcApp.Controllers
{
public class ProductController : Controller
{
protected ILogger Logger;
public ProductController(ILogger logger;)
{
Logger = logger;
}
public ActionResult Index()
{
Logger.Write(LogEntry.Event, Server.SessionID, "Start of '{0}' action call", "Index");
var serviceStopwatch = Stopwatch.StartNew();
Logger.Write(LogEntry.Task, Server.SessionID, "Start of '{0}' task's execution", "GetData");
var data = service.GetData();
serviceStopwatch.Stop();
Logger.Write(LogEntry.Task, Server.SessionID, serviceStopwatch.Elapsed, "End of '{0}' task's execution", "GetData");
var dbCallStopwatch = Stopwatch.StartNew();
Logger.Write(LogEntry.Task, Server.SessionID, "Start of '{0}' db call", "GetObjects");
var objects = repository.GetObjects();
dbCallStopwatch.Stop();
Logger.Write(LogEntry.Task, Server.SessionID, dbCallStopwatch.Elapsed, "End of '{0}' db call", "GetObjects");
Logger.Write(LogEntry.Event, Server.SessionID, "End of '{0}' action call", "Index");
return View();
}
}
}
위의 코드에서 그룹의 모든 항목에 대해 서버의 세션 ID (자동 생성 됨)에서 키의 값을 가져옵니다.
Logger.Write 방법의 서명을 다음과 같이해야한다 :
public void Write(LogEntryType logEntryType, string key, string message, params string[] args)
{
var item = new LogEntry
{
LogEntryType = (int?)logEntryType,
EntryDate = DateTime.Now,
Key = key,
Description = string.Format(message, args)
};
// Code for save log entry to text file, database, send email if it's an error, etc.
}
public void Write(LogEntryType logEntryType, string key, TimeSpan elapsedTime, string message, params string[] args)
{
var item = new LogEntry
{
LogEntryType = (int?)logEntryType,
EntryDate = DateTime.Now,
ElapsedTime = elapsedTime,
Key = key,
Description = string.Format(message, args)
};
// Code for save log entry to text file, database, send email if it's an error, etc.
}
일반적으로 실제 비즈니스 응용 프로그램에서, 우리는 실행 메트릭 및 기타 물건에 대한 워크 플로우 정의가 필요하지만,이 순간에 내가하지 얼마나 복잡한지를 알고이 기능을 개발하고 싶습니다.
모든 로거의 호출을 필요한 지점에 추가하고 모든 것을 데이터베이스 (SQL 또는 nosql)에 저장하면 다음에 하나의 세션 ID 이벤트에 대한 모든 정보가 추출됩니다. 위에서 볼 수 있듯이
, 일부 로그 항목 유형 정의가 있습니다 : 경고 및 오류, 당신이 그것을 기록 할 수 있습니다 예외가있을 경우 내부 catch 블록의 오류 처리를 위해 시도-catch 블록을 추가하는 것이 가정 :
Logger.Write(LogEntry.Error, Server.SessionID, "There was an error on '{0}' task. Details: '{1}'", "Index", ex.Message);
추가 포인트로 비동기 작업을 구현하여 서버가 요청을 차단하지 않도록하는 것이 좋습니다.
이 대답이 개념을 개선 할 수있는 것이라면이 문제를 어떻게 해결할 수 있는지에 대한 기본적인 아이디어입니다.