예제 #1
0
        protected override void Write(LogEventInfo logEvent)
        {
            AppLog log = new AppLog();

            log.AppDomain   = AppDomain.CurrentDomain.FriendlyName;
            log.ClientIP    = this._contextProvider.ClientIP;
            log.Level       = logEvent.Level.Name;
            log.LoggerName  = logEvent.LoggerName;
            log.MachineName = Environment.MachineName;
            log.Message     = logEvent.FormattedMessage;

            StackTrace st = new StackTrace(true);
            StackFrame sf = this.GetRealFrame(st);

            if (sf != null)
            {
                log.Location = string.Format("{0}.{1}:{2}",
                                             sf.GetMethod().ReflectedType.Name,
                                             sf.GetMethod().Name,
                                             sf.GetFileLineNumber());
            }
            else
            {
                log.Location = string.Empty;
            }

            using (IDbContext dbContext = _dbContextFactory.Create())
            {
                if (dbContext.IsInitialized)
                {
                    dbContext.Set <AppLog>().Add(log);
                    dbContext.SaveChanges();
                }
                else
                {
                    //In case , EF dbcontext is not Initialized(configured) yet , log it to file
                    log.CreatedOn = DateTime.UtcNow;
                    File.AppendAllText(_appManager.MapPhysicalFile(FileName),
                                       _xmlSerializer.SerializeToXML(log));
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Saves objects to file
 /// </summary>
 /// <param name="items">Objects to ve saved</param>
 public void Save(List <T> items)
 {
     File.WriteAllText(FilePath, _xmlSerializer.SerializeToXML(items));
 }