MOSS 2007 Maintenance and Trace logs Removal

By default, Office SharePoint Server 2007 trace logging is enabled in Central Administration > Operations > Diagnostic Logging and the log files are stored at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS on your front-end web server.
ManagingMOSS

Without changing the verbosity of logging via event throttling settings, or the number of minutes before creating a new log file, each log file is around 50 MB. By default, MOSS keeps 48 of these files, spanning 2 days for a grand total of around 2.4 GB.

I have changed the default path location to a different drive in the Trace Log section and decrease the number of log files to 12 to be used for 24hrs(1440 min).

Here are the settings on the diagnostic logging page:
Diagnostic Logging

The log file related to SharePoint_Config database can grow to huge sizes without a maintenance plan, or at the very least setting databases to backup in “simple” mode will dramatically reduce the size of the logs.

MOSS 2007 server installation has a log file called WSS_AdminService.log held in C:\Documents and Settings\Default User\Local Settings\Temp. This is just a log file and it is being written at regular intervals increasing its size. Worse still, because it’s in the C:\Documents and Settings\Default User folder it gets duplicated when a new user logs onto the server console. You can safely delete this file.

The following is a C# console application code which can make the administrator work easier. Administrator can create the console application using the following code and schedule it every alternate day to delete the above mentioned logs automatically. This way you can keep your server disk usage low.
—————————————–

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ComponentModel;
  5. using System.Runtime.InteropServices;
  6. namespace TemporaryFilesRemoval
  7. {
  8.     class Program
  9.     {
  10.         /// <summary>
  11.         /// The main entry point for the application.
  12.         /// </summary>
  13.         [STAThread]
  14.         static void Main(string[] args)
  15.         {
  16.             Console.Title = “Temporary LOG Files Removal”;
  17.             DeleteFiles(“C:\\Program Files\\Common Files\\Microsoft Shared\\web server extensions\\12\\LOGS”, “*.log”);
  18.             DeleteFolder(“C:\\Program Files\\Common Files\\Microsoft Shared\\web server extensions\\12\\LOGS”, “*.log”);
  19.             DeleteFiles(“D:\\Sharepoint Logs”, “*.log”);
  20.             DeleteFiles(“C:\\Documents and Settings\\Default User\\Local Settings\\Temp”, “*.log”);
  21.         }
  22.         static void DeleteFolder(string rootfolder, string filter)
  23.         {
  24.             try
  25.             {
  26.                 string[] directories = System.IO.Directory.GetDirectories(rootfolder);
  27.                 Console.WriteLine(string.Format(“Total {0} directories in {1}”, (directories.Length), rootfolder));
  28.                 foreach (string dir in directories)
  29.                 {
  30.                     DeleteFiles(dir, filter);
  31.                     DeleteFolder(dir, filter);
  32.                     try
  33.                     {
  34.                         Console.WriteLine(string.Format(“Deleting directory : {0}”, dir));
  35.                         System.IO.Directory.Delete(dir);
  36.                         Console.WriteLine(string.Format(“{0} has been deleted.”, dir));
  37.                     }
  38.                     catch (Exception ex)
  39.                     {
  40.                         Console.WriteLine(string.Format(“Error : {0}”, ex.Message));
  41.                         Console.WriteLine();
  42.                     }
  43.                 }
  44.             }
  45.             catch (Exception ex)
  46.             {
  47.                 Console.WriteLine(string.Format(“Error : {0}”, ex.Message));
  48.                 Console.WriteLine();
  49.             }
  50.         }
  51.         static void DeleteFiles(string folder, string filter)
  52.         {
  53.             string[] files = null;
  54.             try
  55.             {
  56.                 if (!string.IsNullOrEmpty(filter))
  57.                 {
  58.                     files = System.IO.Directory.GetFiles(folder, filter);
  59.                 }
  60.                 else
  61.                 {
  62.                     files = System.IO.Directory.GetFiles(folder);
  63.                 }
  64.                 Console.WriteLine(string.Format(“Total {0} file(s) in {1}”, (files.Length), folder));
  65.                 Console.WriteLine();
  66.                 foreach (string file in files)
  67.                 {
  68.                     Console.WriteLine(string.Format(“Deleting {0}”, file));
  69.                     System.IO.File.Delete(file);
  70.                     Console.WriteLine(string.Format(“{0} has been deleted.”, file));
  71.                     Console.WriteLine();
  72.                 }
  73.             }
  74.             catch (Exception ex)
  75.             {
  76.                 Console.WriteLine(string.Format(“Error : {0}”, ex.Message));
  77.                 Console.WriteLine();
  78.             }
  79.         }
  80.     }
  81. }
    —————————————–

I have included a word version of it here –LogClean or Alternatively http://technobuff.pastebin.com/f61f70e48

Please note that in the above code, I have also included my new path of trace logs i.e, (“D:\\Sharepoint Logs”, “*.log”);

You can also use the same application to clean up your Temporary ASP.NET Files. Just include the path, default will be C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files

DeleteFiles(“C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\Temporary ASP.NET Files”, string.Empty);
DeleteFolder(“C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\Temporary ASP.NET Files”, string.Empty);