It’s been a little while since I’ve released any new commands so I decided to throw one together real quick. Fellow MVP, Matthew McDermott, was working on a scripted install and had asked if I had anything to set the path for the ULS logs. Unfortunately I didn’t but a quick disassemble of the central admin page showed that the code to do this was really simple so I decided to throw a command together: gl-tracelog.

The code necessary to set the trace log properties is real simple – you just get a SPDiagnosticsService object via the static Local property and then set the necessary properties and call update:

 1using System.Collections.Specialized;
 2using System.Text;
 3using Lapointe.SharePoint.STSADM.Commands.SPValidators;
 4using Microsoft.SharePoint.Administration;
 5using Lapointe.SharePoint.STSADM.Commands.OperationHelpers;
 6 
 7namespace Lapointe.SharePoint.STSADM.Commands.Logging
 8{
 9    public class TraceLog : SPOperation
10    {
11        /// <summary>
12        /// Initializes a new instance of the <see cref="TraceLog"/> class.
13        /// </summary>
14        public TraceLog()
15        {
16            SPDiagnosticsService diagnosticsService = SPDiagnosticsService.Local;
17 
18            SPParamCollection parameters = new SPParamCollection();
19            parameters.Add(new SPParam("logdirectory", "log", false, diagnosticsService.LogLocation, new SPNonEmptyValidator()));
20            parameters.Add(new SPParam("logfilecount", "num", false, diagnosticsService.LogsToKeep.ToString() , new SPIntRangeValidator(0, 1024)));
21            parameters.Add(new SPParam("logfileminutes", "min", false, diagnosticsService.LogCutInterval.ToString(), new SPIntRangeValidator(0, 1440)));
22 
23            StringBuilder sb = new StringBuilder();
24            sb.Append("\r\n\r\nSets the log file location (note that the location must exist on each server) and the maximum number of log files to maintain and how long to capture events to a single file.\r\n\r\nParameters:");
25            sb.Append("\r\n\t[-logdirectory <log file location>]");
26            sb.Append("\r\n\t[-logfilecount <number of log files to create (0-1024)>]");
27            sb.Append("\r\n\t[-logfileminutes <number of minutes to use a log file (0-1440)>]");
28            Init(parameters, sb.ToString());
29        }
30 
31        #region ISPStsadmCommand Members
32 
33        /// <summary>
34        /// Gets the help message.
35        /// </summary>
36        /// <param name="command">The command.</param>
37        /// <returns></returns>
38        public override string GetHelpMessage(string command)
39        {
40            return HelpMessage;
41        }
42 
43        /// <summary>
44        /// Runs the specified command.
45        /// </summary>
46        /// <param name="command">The command.</param>
47        /// <param name="keyValues">The key values.</param>
48        /// <param name="output">The output.</param>
49        /// <returns></returns>
50        public override int Execute(string command, StringDictionary keyValues, out string output)
51        {
52            output = string.Empty;
53 
54            string logDirectory = Params["logdirectory"].Value;
55            int logFileCount = int.Parse(Params["logfilecount"].Value);
56            int logFileMinutes = int.Parse(Params["logfileminutes"].Value);
57 
58            SPDiagnosticsService local = SPDiagnosticsService.Local;
59            local.LogLocation = logDirectory;
60            local.LogsToKeep = logFileCount;
61            local.LogCutInterval = logFileMinutes;
62            local.Update();
63 
64            return OUTPUT_SUCCESS;
65        }
66 
67        #endregion
68    }
69}

The syntax for the command is shown below:

c:\>stsadm -help gl-tracelog

stsadm -o gl-tracelog


Sets the log file location (note that the location must exist on each server) and the maximum number of log files to maintain and how long to capture events to a single file.

Parameters:
        [-logdirectory <log file location>]
        [-logfilecount <number of log files to create (0-1024)>]
        [-logfileminutes <number of minutes to use a log file (0-1440)>]

Here’s an example of how to use the command to set each property:

stsadm -o gl-tracelog -logdirectory c:\moss\logs -logfilecount 100 -logfileminutes 30

One question someone might have is why I would want to change the location of the log files? There are two reasons to do this, performance and disk capacity. It’s often a best practice to keep your C drive dedicated to the operating system and application installs and then to create another another drive for your log files (ULS logs and IIS logs for instance). Doing this will allow each drive to perform with less contention and will allow log files to grow without the risk of bringing down the operating system (let your C drive fill up and see how long your SharePoint server continues to run – eventually you won’t even be able to log in to the machine). Joel Oleson has a good post on this that I’d highly recommend everyone read: SharePoint Disk Allocation and Disk I/O.