Fórum Enterprise Library 4.1 - Log arquivo texto #384375
23/08/2010
Oi,
Faço uso do Enterprise Library para registrar minhas exceções em um arquivo de log. Algumas vezes eu abro o arquivo e tento limpa-lo, mas não consigo, porque ele me diz que o meu arquivo pode estar como somente leitura ou foi escrito por outra aplicação. É como se faltasse dar um "Close" após a escrita no arquivo quando utilizo o método "ExceptionPolicy.HandleException".
Alguém sabe me dizer como posso contornar esse problema?
Posts
Carlos,
Não existe o Close infelizmente. Logo, como o arquivo esta sendo usado pela aplicação, vc pode tentar limitar o escopo do mesmo, usando um using. Ficaria algo assim:
using (new Tracer("Outer most trace", "Outer"))
{
using (new Tracer("A nested trace"))
{
Logger.Write("Hello World inside");
}
using (new Tracer("Another nested trace", "Inner"))
{
Logger.Write("Hello World inside again");
}
}
Ja tentou isto?
Aguardo
Att
Luiz Maia
Para comprovar o que disse, veja este arquivo. Ele poderá ajudar em outra solução, como por exemplo deletar o arquivo e recriá-lo em tempo de execução:
As also suggested by Sarah the code below will reset your configuration. As far as I know it will be reset to the default configuration based on the application's configuration file which if in case you're not utilizing any application config file then it will be reset to none. EnterpriseLibraryContainer.Current
= EnterpriseLibraryContainer.CreateDefaultContainer(ConfigurationSourceFactory.Create());
While if the only reason of removing a trace listener is to only remove the file locked, it is not actually the Trace Listener that keeps the file locked, it is the Logger/LogWriter instance. So if you want to delete the log file during runtime, one thing I can think of on how you can do this is to reset or disposed the Logger/LogWriter instance before you do the actual deletion of the file during runtime (Logger - Logger.Reset(); , LogWriter - writer.Dispose();). Then you can proceed with the next logging execution (if you're using a LogWriter object you need to instantiate its value again before proceeding into the next logging execution). Here's a related thread regarding this http://entlib.codeplex.com/Thread/View.aspx?ThreadId=216051.
24/08/2010
Carlos Nogueira
Então Luiz, eu não sei como poderia fazer uso da palavra-chave "using", porque eu uso simplesmente aquele comando, da classe ExceptionPolicy, e só. Não uso a classe Trace ou Debug para registrar isso no arquivo de log. Você já passou também por este mesmo problema?
Aproveitando o post, você sabe se existe uma maneira de fazer com que o Enterprise Library gere um arquivo de log por dia? O intuito disso, e não deixar que o arquivo de texto para log fique imenso. Apenas uma idéia!
Carlos,
Eu nunca tive esta preocupação até hoje com o tamanho do arquivo de log.
Mas veja que o XML onde armazena os dados do log tem uma tag :rollInterval="Day"
Veja o trecho de codigo completo:
<
configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0" />
</configSections>
<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add fileName="Logs\Report.log" formatter="Progress Formatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0" name="Rolling FlatFile TraceListener" rollInterval="Day" rollFileExistsBehavior="Increment" timeStampPattern="yyyy-MM-dd" />
</listeners>
<formatters>
<add template=" Category: Message: " type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0" name="Progress Formatter" />
</formatters>
<categorySources>
<add switchValue="Information" name="Status">
<listeners>
<add name="Rolling FlatFile TraceListener" />
</listeners>
</add>
</categorySources>
Fonte: http://entlib.codeplex.com/Thread/View.aspx?ThreadId=28188
25/08/2010
Carlos Nogueira
Valeu pela dica Luiz!
Neste caso por enquanto nestr projeto que estou desenvolvendo, vou deixar assim, mas irei simular para os próximos este tipo de coisa para verificar se fica melhor para equipe.
Pode finalizar o chamado, valeu pela força!