File size: 15,319 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Globalization;
using System.Management.Automation.Internal;
namespace System.Management.Automation.Tracing
{
/// <summary>
/// ETW logging API.
/// </summary>
internal static class PSEtwLog
{
#if UNIX
private static readonly PSSysLogProvider provider;
#else
private static readonly PSEtwLogProvider provider;
#endif
/// <summary>
/// Class constructor.
/// </summary>
static PSEtwLog()
{
#if UNIX
provider = new PSSysLogProvider();
#else
provider = new PSEtwLogProvider();
#endif
}
internal static void LogConsoleStartup()
{
Guid activityId = EtwActivity.GetActivityId();
if (activityId == Guid.Empty)
{
EtwActivity.SetActivityId(EtwActivity.CreateActivityId());
}
PSEtwLog.LogOperationalInformation(PSEventId.Perftrack_ConsoleStartupStart, PSOpcode.WinStart,
PSTask.PowershellConsoleStartup, PSKeyword.UseAlwaysOperational);
}
/// <summary>
/// Provider interface function for logging health event.
/// </summary>
/// <param name="logContext"></param>
/// <param name="eventId"></param>
/// <param name="exception"></param>
/// <param name="additionalInfo"></param>
internal static void LogEngineHealthEvent(LogContext logContext, int eventId, Exception exception, Dictionary<string, string> additionalInfo)
{
provider.LogEngineHealthEvent(logContext, eventId, exception, additionalInfo);
}
/// <summary>
/// Provider interface function for logging engine lifecycle event.
/// </summary>
/// <param name="logContext"></param>
/// <param name="newState"></param>
/// <param name="previousState"></param>
internal static void LogEngineLifecycleEvent(LogContext logContext, EngineState newState, EngineState previousState)
{
provider.LogEngineLifecycleEvent(logContext, newState, previousState);
}
/// <summary>
/// Provider interface function for logging command health event.
/// </summary>
/// <param name="logContext"></param>
/// <param name="exception"></param>
internal static void LogCommandHealthEvent(LogContext logContext, Exception exception)
{
provider.LogCommandHealthEvent(logContext, exception);
}
/// <summary>
/// Provider interface function for logging command lifecycle event.
/// </summary>
/// <param name="logContext"></param>
/// <param name="newState"></param>
internal static void LogCommandLifecycleEvent(LogContext logContext, CommandState newState)
{
provider.LogCommandLifecycleEvent(() => logContext, newState);
}
/// <summary>
/// Provider interface function for logging pipeline execution detail.
/// </summary>
/// <param name="logContext"></param>
/// <param name="pipelineExecutionDetail"></param>
internal static void LogPipelineExecutionDetailEvent(LogContext logContext, List<string> pipelineExecutionDetail)
{
provider.LogPipelineExecutionDetailEvent(logContext, pipelineExecutionDetail);
}
/// <summary>
/// Provider interface function for logging provider health event.
/// </summary>
/// <param name="logContext"></param>
/// <param name="providerName"></param>
/// <param name="exception"></param>
internal static void LogProviderHealthEvent(LogContext logContext, string providerName, Exception exception)
{
provider.LogProviderHealthEvent(logContext, providerName, exception);
}
/// <summary>
/// Provider interface function for logging provider lifecycle event.
/// </summary>
/// <param name="logContext"></param>
/// <param name="providerName"></param>
/// <param name="newState"></param>
internal static void LogProviderLifecycleEvent(LogContext logContext, string providerName, ProviderState newState)
{
provider.LogProviderLifecycleEvent(logContext, providerName, newState);
}
/// <summary>
/// Provider interface function for logging AmsiUtil State event.
/// </summary>
/// <param name="state">This the action performed in AmsiUtil class, like init, scan, etc.</param>
/// <param name="context">The amsiContext handled - Session pair.</param>
internal static void LogAmsiUtilStateEvent(string state, string context)
{
provider.LogAmsiUtilStateEvent(state, context);
}
/// <summary>
/// Provider interface function for logging WDAC query event.
/// </summary>
/// <param name="queryName">Name of the WDAC query.</param>
/// <param name="fileName">Name of script file for policy query. Can be null value.</param>
/// <param name="querySuccess">Query call succeed code.</param>
/// <param name="queryResult">Result code of WDAC query.</param>
internal static void LogWDACQueryEvent(
string queryName,
string fileName,
int querySuccess,
int queryResult)
{
provider.LogWDACQueryEvent(queryName, fileName ?? string.Empty, querySuccess, queryResult);
}
/// <summary>
/// Provider interface function for logging WDAC audit event.
/// </summary>
/// <param name="title">Title of WDAC audit event.</param>
/// <param name="message">WDAC audit event message.</param>
/// <param name="fqid">FullyQualifiedId of WDAC audit event.</param>
internal static void LogWDACAuditEvent(
string title,
string message,
string fqid)
{
provider.LogWDACAuditEvent(title, message, fqid);
}
/// <summary>
/// Provider interface function for logging settings event.
/// </summary>
/// <param name="logContext"></param>
/// <param name="variableName"></param>
/// <param name="value"></param>
/// <param name="previousValue"></param>
internal static void LogSettingsEvent(LogContext logContext, string variableName, string value, string previousValue)
{
provider.LogSettingsEvent(logContext, variableName, value, previousValue);
}
/// <summary>
/// Logs information to the operational channel.
/// </summary>
/// <param name="id"></param>
/// <param name="opcode"></param>
/// <param name="task"></param>
/// <param name="keyword"></param>
/// <param name="args"></param>
internal static void LogOperationalInformation(PSEventId id, PSOpcode opcode, PSTask task, PSKeyword keyword, params object[] args)
{
provider.WriteEvent(id, PSChannel.Operational, opcode, PSLevel.Informational, task, keyword, args);
}
/// <summary>
/// Logs information to the operational channel.
/// </summary>
/// <param name="id"></param>
/// <param name="opcode"></param>
/// <param name="task"></param>
/// <param name="keyword"></param>
/// <param name="args"></param>
internal static void LogOperationalWarning(PSEventId id, PSOpcode opcode, PSTask task, PSKeyword keyword, params object[] args)
{
provider.WriteEvent(id, PSChannel.Operational, opcode, PSLevel.Warning, task, keyword, args);
}
/// <summary>
/// Logs Verbose to the operational channel.
/// </summary>
/// <param name="id"></param>
/// <param name="opcode"></param>
/// <param name="task"></param>
/// <param name="keyword"></param>
/// <param name="args"></param>
internal static void LogOperationalVerbose(PSEventId id, PSOpcode opcode, PSTask task, PSKeyword keyword, params object[] args)
{
provider.WriteEvent(id, PSChannel.Operational, opcode, PSLevel.Verbose, task, keyword, args);
}
/// <summary>
/// Logs error message to the analytic channel.
/// </summary>
/// <param name="id"></param>
/// <param name="opcode"></param>
/// <param name="task"></param>
/// <param name="keyword"></param>
/// <param name="args"></param>
internal static void LogAnalyticError(PSEventId id, PSOpcode opcode, PSTask task, PSKeyword keyword, params object[] args)
{
provider.WriteEvent(id, PSChannel.Analytic, opcode, PSLevel.Error, task, keyword, args);
}
/// <summary>
/// Logs warning message to the analytic channel.
/// </summary>
/// <param name="id"></param>
/// <param name="opcode"></param>
/// <param name="task"></param>
/// <param name="keyword"></param>
/// <param name="args"></param>
internal static void LogAnalyticWarning(PSEventId id, PSOpcode opcode, PSTask task, PSKeyword keyword, params object[] args)
{
provider.WriteEvent(id, PSChannel.Analytic, opcode, PSLevel.Warning, task, keyword, args);
}
/// <summary>
/// Logs remoting fragment data to verbose channel.
/// </summary>
/// <param name="id"></param>
/// <param name="opcode"></param>
/// <param name="task"></param>
/// <param name="keyword"></param>
/// <param name="objectId"></param>
/// <param name="fragmentId"></param>
/// <param name="isStartFragment"></param>
/// <param name="isEndFragment"></param>
/// <param name="fragmentLength"></param>
/// <param name="fragmentData"></param>
internal static void LogAnalyticVerbose(PSEventId id, PSOpcode opcode, PSTask task, PSKeyword keyword,
Int64 objectId,
Int64 fragmentId,
int isStartFragment,
int isEndFragment,
UInt32 fragmentLength,
PSETWBinaryBlob fragmentData)
{
if (provider.IsEnabled(PSLevel.Verbose, keyword))
{
string payLoadData = Convert.ToHexString(fragmentData.blob, fragmentData.offset, fragmentData.length);
payLoadData = string.Create(CultureInfo.InvariantCulture, $"0x{payLoadData}");
provider.WriteEvent(id, PSChannel.Analytic, opcode, PSLevel.Verbose, task, keyword,
objectId, fragmentId, isStartFragment, isEndFragment, fragmentLength,
payLoadData);
}
}
/// <summary>
/// Logs verbose message to the analytic channel.
/// </summary>
/// <param name="id"></param>
/// <param name="opcode"></param>
/// <param name="task"></param>
/// <param name="keyword"></param>
/// <param name="args"></param>
internal static void LogAnalyticVerbose(PSEventId id, PSOpcode opcode, PSTask task, PSKeyword keyword, params object[] args)
{
provider.WriteEvent(id, PSChannel.Analytic, opcode, PSLevel.Verbose, task, keyword, args);
}
/// <summary>
/// Logs informational message to the analytic channel.
/// </summary>
/// <param name="id"></param>
/// <param name="opcode"></param>
/// <param name="task"></param>
/// <param name="keyword"></param>
/// <param name="args"></param>
internal static void LogAnalyticInformational(PSEventId id, PSOpcode opcode, PSTask task, PSKeyword keyword, params object[] args)
{
provider.WriteEvent(id, PSChannel.Analytic, opcode, PSLevel.Informational, task, keyword, args);
}
/// <summary>
/// Logs error message to operation channel.
/// </summary>
/// <param name="id"></param>
/// <param name="opcode"></param>
/// <param name="task"></param>
/// <param name="keyword"></param>
/// <param name="args"></param>
internal static void LogOperationalError(PSEventId id, PSOpcode opcode, PSTask task, PSKeyword keyword, params object[] args)
{
provider.WriteEvent(id, PSChannel.Operational, opcode, PSLevel.Error, task, keyword, args);
}
/// <summary>
/// Logs error message to the operational channel.
/// </summary>
/// <param name="id"></param>
/// <param name="opcode"></param>
/// <param name="task"></param>
/// <param name="logContext"></param>
/// <param name="payLoad"></param>
internal static void LogOperationalError(PSEventId id, PSOpcode opcode, PSTask task, LogContext logContext, string payLoad)
{
provider.WriteEvent(id, PSChannel.Operational, opcode, task, logContext, payLoad);
}
internal static void SetActivityIdForCurrentThread(Guid newActivityId)
{
provider.SetActivityIdForCurrentThread(newActivityId);
}
internal static void ReplaceActivityIdForCurrentThread(Guid newActivityId,
PSEventId eventForOperationalChannel, PSEventId eventForAnalyticChannel, PSKeyword keyword, PSTask task)
{
// set the new activity id
provider.SetActivityIdForCurrentThread(newActivityId);
// Once the activity id is set, write the transfer event
WriteTransferEvent(newActivityId, eventForOperationalChannel, eventForAnalyticChannel, keyword, task);
}
/// <summary>
/// Writes a transfer event mapping current activity id
/// with a related activity id
/// This function writes a transfer event for both the
/// operational and analytic channels.
/// </summary>
/// <param name="relatedActivityId"></param>
/// <param name="eventForOperationalChannel"></param>
/// <param name="eventForAnalyticChannel"></param>
/// <param name="keyword"></param>
/// <param name="task"></param>
internal static void WriteTransferEvent(Guid relatedActivityId, PSEventId eventForOperationalChannel,
PSEventId eventForAnalyticChannel, PSKeyword keyword, PSTask task)
{
provider.WriteEvent(eventForOperationalChannel, PSChannel.Operational, PSOpcode.Method, PSLevel.Informational, task,
PSKeyword.UseAlwaysOperational);
provider.WriteEvent(eventForAnalyticChannel, PSChannel.Analytic, PSOpcode.Method, PSLevel.Informational, task,
PSKeyword.UseAlwaysAnalytic);
}
/// <summary>
/// Writes a transfer event.
/// </summary>
/// <param name="parentActivityId"></param>
internal static void WriteTransferEvent(Guid parentActivityId)
{
provider.WriteTransferEvent(parentActivityId);
}
}
}
|