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

  • SEARCH
  • Home
  • 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

Ask A Question

Stats

  • Questions 418k
  • Answers 418k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In IE 5.5 and earlier, and in later versions of… May 15, 2026 at 9:51 am
  • Editorial Team
    Editorial Team added an answer First of all, make sure that your name_l column is… May 15, 2026 at 9:51 am
  • Editorial Team
    Editorial Team added an answer This field is available through API like any other. Your… May 15, 2026 at 9:51 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.