Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 803711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:48:12+00:00 2026-05-14T23:48:12+00:00

I need to manage the trace files for a database on Sql Server 2005

  • 0

I need to manage the trace files for a database on Sql Server 2005 Express Edition. The C2 audit logging is turned on for the database, and the files that it’s creating are eating up a lot of space.

Can this be done from within Sql Server, or do I need to write a service to monitor these files and take the appropriate actions?

I found the [master].[sys].[trace] table with the trace file properties. Does anyone know the meaning of the fields in this table?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:48:14+00:00Added an answer on May 14, 2026 at 11:48 pm

    Here’s what I came up with that is working pretty good from a console application:

        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("CcmLogManager v1.0");
                Console.WriteLine();
    
                // How long should we keep the files around (in months) 12 is the PCI requirement?
                var months = Convert.ToInt32(ConfigurationManager.AppSettings.Get("RemoveMonths") ?? "12");
    
                var currentFilePath = GetCurrentAuditFilePath();
    
                Console.WriteLine("Path: {0}", new FileInfo(currentFilePath).DirectoryName);
                Console.WriteLine();
    
                Console.WriteLine("------- Removing Files --------------------");
    
                var fileInfo = new FileInfo(currentFilePath);
                if (fileInfo.DirectoryName != null)
                {
                    var purgeBefore = DateTime.Now.AddMonths(-months);
                    var files = Directory.GetFiles(fileInfo.DirectoryName, "audittrace*.trc.zip");
    
                    foreach (var file in files)
                    {
                        try
                        {
                            var fi = new FileInfo(file);
    
                            if (PurgeLogFile(fi, purgeBefore))
                            {
                                Console.WriteLine("Deleting: {0}", fi.Name);
    
                                try
                                {
                                    fi.Delete();
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                        }
                    }
                }
    
                Console.WriteLine("------- Files Removed ---------------------");
                Console.WriteLine();
    
    
                Console.WriteLine("------- Compressing Files -----------------");
    
                if (fileInfo.DirectoryName != null)
                {
                    var files = Directory.GetFiles(fileInfo.DirectoryName, "audittrace*.trc");
    
                    foreach (var file in files)
                    {
                        // Don't attempt to compress the current log file.
                        if (file.ToLower() == fileInfo.FullName.ToLower())
                            continue;
    
                        var zipFileName = file + ".zip";
    
                        var fi = new FileInfo(file);
                        var zipEntryName = fi.Name;
    
                        Console.WriteLine("Zipping: \"{0}\"", fi.Name);
    
                        try
                        {
                            using (var fileStream = File.Create(zipFileName))
                            {
                                var zipFile = new ZipOutputStream(fileStream);
                                zipFile.SetLevel(9);
    
                                var zipEntry = new ZipEntry(zipEntryName);
                                zipFile.PutNextEntry(zipEntry);
    
                                using (var ostream = File.OpenRead(file))
                                {
                                    int bytesRead;
                                    var obuffer = new byte[2048];
                                    while ((bytesRead = ostream.Read(obuffer, 0, 2048)) > 0)
                                        zipFile.Write(obuffer, 0, bytesRead);
                                }
    
                                zipFile.Finish();
                                zipFile.Close();
                            }
    
                            fi.Delete();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                        }
                    }
                }
    
                Console.WriteLine("------- Files Compressed ------------------");
                Console.WriteLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
    
            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }
    
        public static bool PurgeLogFile(FileInfo fi, DateTime purgeBefore)
        {
            try
            {
                var filename = fi.Name;
                if (filename.StartsWith("audittrace"))
                {
                    filename = filename.Substring(10, 8);
    
                    var year = Convert.ToInt32(filename.Substring(0, 4));
                    var month = Convert.ToInt32(filename.Substring(4, 2));
                    var day = Convert.ToInt32(filename.Substring(6, 2));
    
                    var logDate = new DateTime(year, month, day);
    
                    return logDate.Date <= purgeBefore.Date;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
    
            return false;
        }
    
        public static string GetCurrentAuditFilePath()
        {
            const string connStr = "Data Source=.\\SERVER;Persist Security Info=True;User ID=;Password=";
    
            var dt = new DataTable();
    
            var adapter =
                new SqlDataAdapter(
                    "SELECT path FROM [master].[sys].[traces] WHERE path like '%audittrace%'", connStr);
            try
            {
                adapter.Fill(dt);
    
                if (dt.Rows.Count >= 1)
                {
                    if (dt.Rows.Count > 1)
                        Console.WriteLine("More than one audit trace file defined!  Count: {0}", dt.Rows.Count);
    
                    var path = dt.Rows[0]["path"].ToString();
                    return path.StartsWith("\\\\?\\") ? path.Substring(4) : path;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
    
            throw new Exception("No Audit Trace File in sys.traces!");
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a app that need manage money datatype. I'm new on Obj-c, so
We have a need to manage a large number (approx 20+) languages for our
I need a software to manage configurations of linux servers in one central location.
I've got a couple DNN portals I manage and I need a solution to
I need to a script that will pull up the Task Manager in Vista
I need to create a photo gallery service that is managed by users. I've
I need to build a managed DLL, targeted for x64, and expose it via
I need to host and run managed controls inside of a purely unmanaged C++
I've managed to mostly ignore all this multi-byte character stuff, but now I need
I have a non-visual component which manages other visual controls. I need to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.