MOSS MVP

I've moved my blog to http://blog.falchionconsulting.com!. Please update your links. This blog is no longer in use--you can find all posts and comments at my new blog; I will no longer be posting to this site and comments have been disabled.

Wednesday, June 25, 2008

Trace Log Settings

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:

   1: using System.Collections.Specialized;
   2: using System.Text;
   3: using Lapointe.SharePoint.STSADM.Commands.SPValidators;
   4: using Microsoft.SharePoint.Administration;
   5: using Lapointe.SharePoint.STSADM.Commands.OperationHelpers;
   6:  
   7: namespace 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.

4 comments:

Anonymous said...

Error During Trace Log Creating WSS3.0
-----------------------------------


Event Type: Error

Event Source: Windows SharePoint Services 3

Event Category: Unified Logging Service

Event ID: 5400

Date: 6/6/2008

Time: 2:06:52 PM

User: N/A

Computer: main-server

Description:

Tracing Service failed to create the trace log file directory 'D:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\LOGS'. Error 3: The system cannot find the path specified.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3525451&SiteID=1

Matthew Chestnut said...

This is just the command I've been looking for, Gary, to assist with my scripted installs. Thank you!

Matthew said...

Thanks Gary...now if there was only a way to set the location of the UAP logs too...hmm

:) YOU ROCK!

Gary Lapointe said...

Matthew - got that one already :) - http://stsadm.blogspot.com/2007/12/set-usage-analysis.html