本文整理汇总了C#中NLog.Config.InstallationContext类的典型用法代码示例。如果您正苦于以下问题:C# InstallationContext类的具体用法?C# InstallationContext怎么用?C# InstallationContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
InstallationContext类属于NLog.Config命名空间,在下文中一共展示了InstallationContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static int Main(string[] args)
{
try
{
using (var context = new InstallationContext())
{
context.LogOutput = Console.Out;
XmlLoggingConfiguration configuration = null;
bool uninstallMode = false;
for (int i = 0; i < args.Length; ++i)
{
switch (args[i])
{
case "-q":
context.LogOutput = TextWriter.Null;
break;
case "-consolelog":
context.LogOutput = Console.Out;
break;
case "-loglevel":
context.LogLevel = LogLevel.FromString(args[++i]);
break;
case "-i":
context.IgnoreFailures = true;
break;
case "-log":
context.LogOutput = File.CreateText(args[++i]);
break;
case "-p":
string arg = args[++i];
int p = arg.IndexOf('=');
if (p < 0)
{
Console.WriteLine("Parameter '{0}' must be NAME=VALUE", arg);
Usage();
return 1;
}
string paramName = arg.Substring(0, p);
string paramValue = arg.Substring(p + 1);
context.Parameters.Add(paramName, paramValue);
break;
case "-u":
uninstallMode = true;
break;
case "-?":
Usage();
return 0;
default:
if (args[i].StartsWith("-"))
{
Usage();
return 1;
}
configuration = new XmlLoggingConfiguration(args[i]);
break;
}
}
if (configuration == null)
{
Usage();
return 1;
}
if (uninstallMode)
{
configuration.Uninstall(context);
}
else
{
configuration.Install(context);
}
return 0;
}
}
catch (Exception ex)
{
Console.WriteLine("EXCEPTION: {0}", ex);
return 1;
}
}
示例2: Uninstall
/// <summary>
/// Performs uninstallation which requires administrative permissions.
/// </summary>
/// <param name="installationContext">The installation context.</param>
public void Uninstall(InstallationContext installationContext)
{
EventLog.DeleteEventSource(this.Source, this.MachineName);
}
示例3: IsInstalled
/// <summary>
/// Determines whether the item is installed.
/// </summary>
/// <param name="installationContext">The installation context.</param>
/// <returns>
/// Value indicating whether the item is installed or null if it is not possible to determine.
/// </returns>
public bool? IsInstalled(InstallationContext installationContext)
{
return EventLog.SourceExists(this.Source, this.MachineName);
}
示例4: EnsureInitialized
/// <summary>
/// Ensures that the performance counter has been initialized.
/// </summary>
/// <returns>True if the performance counter is operational, false otherwise.</returns>
private bool EnsureInitialized()
{
if (!this.initialized)
{
this.initialized = true;
if (this.AutoCreate)
{
using (var context = new InstallationContext())
{
this.Install(context);
}
}
try
{
this.perfCounter = new PerformanceCounter(this.CategoryName, this.CounterName, this.InstanceName, false);
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
{
throw;
}
InternalLogger.Error("Cannot open performance counter {0}/{1}/{2}: {3}", this.CategoryName, this.CounterName, this.InstanceName, exception);
}
}
return this.perfCounter != null;
}
示例5: Install
/// <summary>
/// Performs installation which requires administrative permissions.
/// </summary>
/// <param name="installationContext">The installation context.</param>
public void Install(InstallationContext installationContext)
{
if (EventLog.SourceExists(this.Source, this.MachineName))
{
string currentLogName = EventLog.LogNameFromSourceName(this.Source, this.MachineName);
if (currentLogName != this.Log)
{
// re-create the association between Log and Source
EventLog.DeleteEventSource(this.Source, this.MachineName);
var escd = new EventSourceCreationData(this.Source, this.Log)
{
MachineName = this.MachineName
};
EventLog.CreateEventSource(escd);
}
}
else
{
var escd = new EventSourceCreationData(this.Source, this.Log)
{
MachineName = this.MachineName
};
EventLog.CreateEventSource(escd);
}
}
示例6: IsInstalled
/// <summary>
/// Determines whether the item is installed.
/// </summary>
/// <param name="installationContext">The installation context.</param>
/// <returns>
/// Value indicating whether the item is installed or null if it is not possible to determine.
/// </returns>
public bool? IsInstalled(InstallationContext installationContext)
{
if (!PerformanceCounterCategory.Exists(this.CategoryName))
{
return false;
}
return PerformanceCounterCategory.CounterExists(this.CounterName, this.CategoryName);
}
示例7: Uninstall
/// <summary>
/// Performs uninstallation which requires administrative permissions.
/// </summary>
/// <param name="installationContext">The installation context.</param>
public void Uninstall(InstallationContext installationContext)
{
string categoryName = this.CategoryName;
if (PerformanceCounterCategory.Exists(categoryName))
{
installationContext.Debug("Deleting category '{0}'", categoryName);
PerformanceCounterCategory.Delete(categoryName);
}
else
{
installationContext.Debug("Category '{0}' does not exist.", categoryName);
}
}
示例8: RunInstallCommands
private void RunInstallCommands(InstallationContext installationContext, IEnumerable<DatabaseCommandInfo> commands)
{
// create log event that will be used to render all layouts
LogEventInfo logEvent = installationContext.CreateLogEvent();
try
{
foreach (var commandInfo in commands)
{
string cs;
if (commandInfo.ConnectionString != null)
{
// if there is connection string specified on the command info, use it
cs = commandInfo.ConnectionString.Render(logEvent);
}
else if (this.InstallConnectionString != null)
{
// next, try InstallConnectionString
cs = this.InstallConnectionString.Render(logEvent);
}
else
{
// if it's not defined, fall back to regular connection string
cs = this.BuildConnectionString(logEvent);
}
this.EnsureConnectionOpen(cs);
var command = this.activeConnection.CreateCommand();
command.CommandType = commandInfo.CommandType;
command.CommandText = commandInfo.Text.Render(logEvent);
try
{
installationContext.Trace("Executing {0} '{1}'", command.CommandType, command.CommandText);
command.ExecuteNonQuery();
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
{
throw;
}
if (commandInfo.IgnoreFailures || installationContext.IgnoreFailures)
{
installationContext.Warning(exception.Message);
}
else
{
installationContext.Error(exception.Message);
throw;
}
}
}
}
finally
{
this.CloseConnection();
}
}
示例9: Install
/// <summary>
/// Performs installation which requires administrative permissions.
/// </summary>
/// <param name="installationContext">The installation context.</param>
public void Install(InstallationContext installationContext)
{
// categories must be installed together, so we must find all PerfCounter targets in the configuration file
var countersByCategory = this.LoggingConfiguration.AllTargets.OfType<PerformanceCounterTarget>().BucketSort(c => c.CategoryName);
string categoryName = this.CategoryName;
if (countersByCategory[categoryName].Any(c => c.created))
{
installationContext.Trace("Category '{0}' has already been installed.", categoryName);
return;
}
try
{
PerformanceCounterCategoryType categoryType;
CounterCreationDataCollection ccds = GetCounterCreationDataCollection(countersByCategory[this.CategoryName], out categoryType);
if (PerformanceCounterCategory.Exists(categoryName))
{
installationContext.Debug("Deleting category '{0}'", categoryName);
PerformanceCounterCategory.Delete(categoryName);
}
installationContext.Debug("Creating category '{0}' with {1} counter(s) (Type: {2})", categoryName, ccds.Count, categoryType);
foreach (CounterCreationData c in ccds)
{
installationContext.Trace(" Counter: '{0}' Type: ({1}) Help: {2}", c.CounterName, c.CounterType, c.CounterHelp);
}
PerformanceCounterCategory.Create(categoryName, "Category created by NLog", categoryType, ccds);
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
{
throw;
}
if (installationContext.IgnoreFailures)
{
installationContext.Warning("Error creating category '{0}': {1}", categoryName, exception.Message);
}
else
{
installationContext.Error("Error creating category '{0}': {1}", categoryName, exception.Message);
throw;
}
}
finally
{
foreach (var t in countersByCategory[categoryName])
{
t.created = true;
}
}
}
示例10: Uninstall
/// <summary>
/// Performs uninstallation which requires administrative permissions.
/// </summary>
/// <param name="installationContext">The installation context.</param>
public void Uninstall(InstallationContext installationContext)
{
this.RunInstallCommands(installationContext, this.UninstallDdlCommands);
}
示例11: IsInstalled
/// <summary>
/// Determines whether the item is installed.
/// </summary>
/// <param name="installationContext">The installation context.</param>
/// <returns>
/// Value indicating whether the item is installed or null if it is not possible to determine.
/// </returns>
public bool? IsInstalled(InstallationContext installationContext)
{
return null;
}
示例12: IsInstalled
/// <summary>
/// Determines whether the item is installed.
/// </summary>
/// <param name="installationContext">The installation context.</param>
/// <returns>
/// Value indicating whether the item is installed or null if it is not possible to determine.
/// </returns>
public bool? IsInstalled(InstallationContext installationContext)
{
var fixedSource = GetFixedSource();
if (!string.IsNullOrEmpty(fixedSource))
{
return EventLog.SourceExists(fixedSource, this.MachineName);
}
InternalLogger.Debug("Unclear if event source exists because it contains layout renderers");
return null; //unclear!
}
示例13: Uninstall
/// <summary>
/// Performs uninstallation which requires administrative permissions.
/// </summary>
/// <param name="installationContext">The installation context.</param>
public void Uninstall(InstallationContext installationContext)
{
var fixedSource = GetFixedSource();
if (string.IsNullOrEmpty(fixedSource))
{
InternalLogger.Debug("Skipping removing of event source because it contains layout renderers");
}
else
{
EventLog.DeleteEventSource(fixedSource, this.MachineName);
}
}
示例14: Install
/// <summary>
/// Performs installation which requires administrative permissions.
/// </summary>
/// <param name="installationContext">The installation context.</param>
public void Install(InstallationContext installationContext)
{
var fixedSource = GetFixedSource();
//always throw error to keep backwardscomp behavior.
CreateEventSourceIfNeeded(fixedSource, true);
}
示例15: Uninstall
/// <summary>
/// Uninstalls target-specific objects from current system.
/// </summary>
/// <param name="installationContext">The installation context.</param>
/// <remarks>
/// Uninstallation typically runs with administrative permissions.
/// </remarks>
public void Uninstall(InstallationContext installationContext)
{
if (installationContext == null)
{
throw new ArgumentNullException("installationContext");
}
this.InitializeAll();
foreach (IInstallable installable in EnumerableHelpers.OfType<IInstallable>(this.configItems))
{
installationContext.Info("Uninstalling '{0}'", installable);
try
{
installable.Uninstall(installationContext);
installationContext.Info("Finished uninstalling '{0}'.", installable);
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
{
throw;
}
installationContext.Error("Uninstallation of '{0}' failed: {1}.", installable, exception);
}
}
}