Search code examples
batch-filebackupevent-log

Simple way to backup event log on Windows Server


Is it possibile to create a simple way to backup the event log, with such as a batch file or a simple app ? I need to make it working on a customer's site, where the reference is an non-expert user. Thanks


Solution

  • Finally I made a little winapp using this method found on the internet:

    public void DoBackup(string sLogName)
    {
        string sBackup = sLogName;  // could be for example "Application"
        EventLog log = new EventLog();
        log.Source = sBackup;
    
        var query = from EventLogEntry entry in log.Entries
                    orderby entry.TimeGenerated descending
                    select entry;
    
        string sBackupName = sBackup+"Log";
        var xml = new XDocument(
            new XElement(sBackupName,
                from EventLogEntry entry in log.Entries
                orderby entry.TimeGenerated descending
                select new XElement("Log",
                  new XElement("Message", entry.Message),
                  new XElement("TimeGenerated", entry.TimeGenerated),
                  new XElement("Source", entry.Source),
                  new XElement("EntryType", entry.EntryType.ToString())
                )
              )
            );
    
        DateTime oggi = DateTime.Now;
        string sToday = DateTime.Now.ToString("yyyyMMdd_hhmmss");
        string path = String.Format("{0}_{1}.xml", sBackupName, sToday);
        xml.Save(Path.Combine(Environment.CurrentDirectory, path));
    }
    

    this is the source link:

    It simply works great!