我有许多工作者角色项目,我想利用log4net功能来记录信息。遗憾的是,我的日志中没有一个实际出现在我的输出窗口中。
我在调试器中跳过一个日志行,输出窗口反而吐出以下行:
' WaWorkerHost.exe' (CLR v4.0.30319:RdRuntime):已加载 ' C:\ Windows \ Microsoft.Net \组件\ GAC_MSIL \ System.Runtime.Caching \ v4.0_4.0.0.0__b03f5f7f11d50a3a \ System.Runtime.Caching.dll&#39 ;. 跳过加载符号。模块已优化并具有调试器选项 ' Just My Code'已启用。
看到这是我的代码,我很困惑为什么我看到这个例外。下面是我的日志app.config设置:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Montetary.Agents.HappyBirthday.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<system.diagnostics>
<trace>
<listeners>
<add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
<filter type="" />
</add>
</listeners>
</trace>
</system.diagnostics>
<log4net>
<appender name="AzureTraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- can be any pattern you like -->
<conversionPattern value="%logger - %message" />
</layout>
</appender>
<!-- does not have to be at the root level -->
<root>
<level value="ALL" />
<appender-ref ref="AzureTraceAppender" />
</root>
</log4net>
我尝试按照此question中的示例进行操作,但结果是相同的
答案 0 :(得分:1)
您可以查看一些内容:
在写入日志文件之前是否调用了log4net configure(只有一次就足够了):
static ManualResetEvent _quitEvent = new ManualResetEvent(false);
public static void Main(string[] args)
{
IServiceCollection services = new ServiceCollection();
ConfigureServices(services);
var serviceProvider = services.BuildServiceProvider();
Console.WriteLine($"[{DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss")}] -> Worker role started");
var listener = serviceProvider.GetService<IMessageProcessor>();
Console.CancelKeyPress += (sender, eArgs) =>
{
listener.OnStop();
Console.WriteLine($"[{DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss")}] -> Worker role finished");
_quitEvent.Set();
eArgs.Cancel = true;
};
_quitEvent.WaitOne();
}
private static IConfigurationRoot GetConfiguration()
{
// Build appsetting.json configuration
var environment = Environment.GetEnvironmentVariable("Environment");
return new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true)
.AddEnvironmentVariables().Build();
}
private static void ConfigureServices(IServiceCollection services)
{
IConfigurationRoot configuration = GetConfiguration();
services.AddSingleton<IConfigurationRoot>(configuration);
// Support typed options
services.AddOptions();
services.Configure<RabbitMQSettings>(configuration.GetSection("RabbitMQConfig"));
services.AddSingleton<IQueueConsumer, QueueConsumer>();
services.AddScoped<IMessageProcessor, MessageProcessor>();
}
}
接下来要为您的配置添加刷新功能:
log4net.Config.XmlConfigurator();
这将立即刷新消息。
确保已将Azure Diagnostics配置为所有用于调试的信息。
然后您可以启用调试内部log4net调试。见internal debugging on this log4net faq page。标准它应该记录到您配置的监听器。将autoflush =“true”选项添加到trace元素。或者找到您可以写入的辅助角色的目录,并访问以读取您的日志。