winsw/doc/loggingAndErrorReporting.md

116 lines
4.2 KiB
Markdown
Raw Normal View History

2018-11-27 16:00:00 +00:00
# Logging and error reporting
2018-11-27 16:00:00 +00:00
## Logging
Winsw supports several different ways to capture stdout and stderr from the process you launch.
2018-11-27 16:00:00 +00:00
## Log directory
2018-11-29 16:00:00 +00:00
The `<logpath>` element specifies the directory in which the log files are created. If this element is absent, it'll default to the same directory where the configuration file resides.
2018-11-27 16:00:00 +00:00
## Append mode (default)
2018-11-29 16:00:00 +00:00
In this mode, *myapp.out.log* and *myapp.err.log* (where *myapp* is the base name of the executable and the configuration file) are created and outputs are simply appended to these files. Note that the file can get quite big.
```xml
<log mode="append"/>
```
2018-11-27 16:00:00 +00:00
## Reset mode
2018-11-29 16:00:00 +00:00
Works like the append mode, except that every time the service starts, the old log files are truncated.
2018-11-29 16:00:00 +00:00
```xml
<log mode="reset"/>
```
2018-11-27 16:00:00 +00:00
## Ignore mode
2018-11-29 16:00:00 +00:00
Throw away stdout and stderr, and do not produce any log files at all.
2018-11-29 16:00:00 +00:00
```xml
<log mode="none"/>
```
## Roll mode
Works like the append mode, but in addition, if the log file gets bigger than a set size, it gets rolled to *myapp.1.out.log*, *myapp.2.out.log* and so on. The nested `<sizeThreshold>` element specifies the rotation threshold in KB (defaults to 10MB), and the nested `<keepFiles>` element specifies the number of rolled files to keep (defaults to 8.)
2018-11-29 16:00:00 +00:00
```xml
<log mode="roll-by-size">
<sizeThreshold>10240</sizeThreshold>
<keepFiles>8</keepFiles>
</log>
```
## Roll by time mode
2018-11-29 16:00:00 +00:00
Works like the roll mode, except that instead of using the size as a threshold, use the time period as the threshold.
This configuration must accompany a nested `<pattern>` element, which specifies the timestamp pattern used as the log file name.
2018-11-29 16:00:00 +00:00
```xml
<log mode="roll-by-time">
<pattern>yyyyMMdd</pattern>
</log>
```
2019-01-24 16:00:00 +00:00
The syntax of the pattern string is specified by [DateTime.ToString(String)](https://docs.microsoft.com/dotnet/api/system.datetime.tostring#System_DateTime_ToString_System_String_).
For example, in the above example, the log of Jan 1, 2013 gets written to `myapp.20130101.out.log` and `myapp.20130101.err.log`.
## Roll by size and time mode
2018-11-29 16:00:00 +00:00
Works in a combination of roll size mode and roll time mode, if the log file gets bigger than a set size, it gets rolled using `<pattern>` provided.
2018-11-29 16:00:00 +00:00
```xml
<log mode="roll-by-size-time">
<sizeThreshold>10240</sizeThreshold>
<pattern>yyyyMMdd</pattern>
<autoRollAtTime>00:00:00</autoRollAtTime>
</log>
```
2019-01-24 16:00:00 +00:00
The syntax of the pattern string is specified by [DateTime.ToString(String)](https://docs.microsoft.com/dotnet/api/system.datetime.tostring#System_DateTime_ToString_System_String_).
For example, in the above example, the log of Jan 1, 2013 gets written to `myapp.20130101.out.log` and `myapp.20130101.err.log`.
2019-01-24 16:00:00 +00:00
The syntax of the autoRollAtTime is specified by [TimeSpan.ToString(String)](https://docs.microsoft.com/dotnet/api/system.timespan.tostring#System_TimeSpan_ToString_System_String_).
For example, in the above example, at the start of the day it will roll the file over.
### Automatic archiving of logs
:warning: This feature is reported to be broken in recent WinSW versions.
It is a potential subject for removal.
```xml
<log mode="roll-by-size-time">
<zipOlderThanNumDays>5</zipOlderThanNumDays>
<zipDateFormat>yyyyMM</zipDateFormat>
</log>
```
2018-11-29 16:00:00 +00:00
The `zipOlderThanNumDays` can only be used in conjection with autoRollAtTime, provide the number of days of files to keep.
```xml
<log mode="roll-by-size-time">
<autoRollAtTime>00:00:00</autoRollAtTime>
<zipOlderThanNumDays>5</zipOlderThanNumDays>
</log>
Added Support for the log appender roll-by-size-time to zip older files (#259) * Introduced the following new elements. 1. logname - you can override the name of the log file rather than using the EXE name, this means you don't have to call your EXE a different name, just name the winsw exe different. Default's the name to the EXE as before. 2. outfiledisabled - you can disable writing to the out file. Defaults to false. 3. errfiledisabled - you can disable writing to the error file. Defaults to false. 4. outfilepattern - you can choose the pattern of the out file. Defaults to .out.log. 5. errfilepattern - you can choos the pattern of the error file. Defaults to .err.log. * Downgraded from C#7.0 syntax. * Applied reviewers comment * not required * removed the key * Added unit test for new fields logname, outfiledisabled, errfiledisabled and errfilepattern. Created a new appender called roll-by-size-time see class RollingSizeTimeLogAppender, this appender supports rolling by time and size and rolling at a specific time each day. Added unit test for the new appender. Added a new option testwait which is similar to test but waits for the user to press any key before calling the stop method. * Update loggingAndErrorReporting.md * Cannot use $ string.format syntax, downgraded code to string.format. * Another syntax found of $ * Fixed a unit tests * Added support to zip files. * Added error handling * Removed the zip call at startup. * Fix issue with UTC * Update loggingAndErrorReporting.md Documented the new fields zipolderthannumdays and zipdateformat * Update loggingAndErrorReporting.md * Applied Code review * Fixed a BST bug * Added zip lib
2018-06-19 16:39:03 +00:00
```
2018-11-29 16:00:00 +00:00
2019-01-24 16:00:00 +00:00
The zipDateFormat can only be used in conjection with autoRollAtTime, provide the zip file format using the [TimeSpan.ToString(String)](https://docs.microsoft.com/dotnet/api/system.timespan.tostring#System_TimeSpan_ToString_System_String_).
2018-11-29 16:00:00 +00:00
```xml
<log mode="roll-by-size-time">
<autoRollAtTime>00:00:00</autoRollAtTime>
<zipDateFormat>yyyyMM</zipDateFormat>
</log>
Added Support for the log appender roll-by-size-time to zip older files (#259) * Introduced the following new elements. 1. logname - you can override the name of the log file rather than using the EXE name, this means you don't have to call your EXE a different name, just name the winsw exe different. Default's the name to the EXE as before. 2. outfiledisabled - you can disable writing to the out file. Defaults to false. 3. errfiledisabled - you can disable writing to the error file. Defaults to false. 4. outfilepattern - you can choose the pattern of the out file. Defaults to .out.log. 5. errfilepattern - you can choos the pattern of the error file. Defaults to .err.log. * Downgraded from C#7.0 syntax. * Applied reviewers comment * not required * removed the key * Added unit test for new fields logname, outfiledisabled, errfiledisabled and errfilepattern. Created a new appender called roll-by-size-time see class RollingSizeTimeLogAppender, this appender supports rolling by time and size and rolling at a specific time each day. Added unit test for the new appender. Added a new option testwait which is similar to test but waits for the user to press any key before calling the stop method. * Update loggingAndErrorReporting.md * Cannot use $ string.format syntax, downgraded code to string.format. * Another syntax found of $ * Fixed a unit tests * Added support to zip files. * Added error handling * Removed the zip call at startup. * Fix issue with UTC * Update loggingAndErrorReporting.md Documented the new fields zipolderthannumdays and zipdateformat * Update loggingAndErrorReporting.md * Applied Code review * Fixed a BST bug * Added zip lib
2018-06-19 16:39:03 +00:00
```
2018-11-27 16:00:00 +00:00
## Error reporting
2018-11-29 16:00:00 +00:00
WinSW uses WMI underneath, and as such it uses its error code as the exit code.
2019-01-24 16:00:00 +00:00
For the complete list of exit codes, see [return values of the Create method of the Win32_Service class](https://docs.microsoft.com/windows/win32/cimwin32prov/create-method-in-class-win32-service#return-value).
2016-11-25 20:56:39 +00:00
When winsw is running as a service, more detailed error information is reported to the Windows event log.