Q:

C# Get SQL Logs

I've searched extensively for help but have found nothing, what I'm trying to do is backup the SQL log files (Management > SQL Server Logs) to a csv or text file.
Is there a way to do this with C#?

A:

From http://technet.microsoft.com/en-us/library/ms187885(v=sql.105).aspx

View the SQL Server error log by using SQL  Management Studio or any
  text editor. By default, the error log is located at Program
  Files\Microsoft SQL Server\MSSQL.n\MSSQL\LOG\ERRORLOG and ERRORLOG.n
  files.

So you can use the Directory.EnumerateFiles class to enumerate the files and File.ReadAllText to read the text from each file.

A:

You should be able to get the error log path via the SQL Server SMO assembly:
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;

namespace SQLLogsToText
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            string errorLogPath = null;

            using (var sqlConnection = new SqlConnection(@"Integrated Security=SSPI; Data Source=(local)\SQLEXPRESS"))
            {
                var serverConnection = new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConnection);
                var server = new Server(serverConnection);
                errorLogPath = server.ErrorLogPath;
            }

            if (errorLogPath != null)
            {
                // Enumerate Files
            }
        }
    }
}

