Cassie Mod

Cassie Mod

  • NA
  • 488
  • 70.5k

can't email log file from log4net , file already in use

Dec 24 2015 7:19 AM
Ive got a logger implemented from log4net wich works fine and creates a nice log file calles logfile.24.12.2015.log and is called like:
LogManagerBase _Log = new LogManagerBase(null);
_Log.Info(class , message); 
 
However when i want to email the log file ( see void downstairs):
 
public void Send(string body, string path, LogManagerBase _Log)
{
var username = "xxxx@xxxx";
var password = "xxxxx";
SmtpCreds = new System.Net.NetworkCredential(username, password);
try
{
var sendTo = username;
var sendFrom = username;
Client.Host = "smtpserver";
var subject = "Log file last executed batch";
Client.Port = 587;
Client.UseDefaultCredentials = false;
Client.Credentials = SmtpCreds;
Client.EnableSsl = false;
MailAddress to = new MailAddress(sendTo);
MailAddress from = new MailAddress(sendFrom);
Msg.Subject = subject;
Msg.Body = body;
Msg.From = from;
Msg.To.Add(to);
if (path != null)
{
Msg.Attachments.Add(new Attachment(path));
}
Client.Send(Msg);
}
catch (Exception ex)
{
_Log.Exception(ex);
}
}
 
It gives the following error:
2015-12-24 13:09:18,162 FATAL - System.IO.IOException: Kan geen toegang krijgen tot het bestand C:\Log\LastExecutedBatch.Cmd_2015.12.24.log omdat het wordt gebruikt door een ander proces.
bij System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
bij System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
bij System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
bij System.Net.Mail.AttachmentBase.SetContentFromFile(String fileName, String mediaType)
bij System.Net.Mail.AttachmentBase..ctor(String fileName)
bij System.Net.Mail.Attachment..ctor(String fileName)
bij LastExecutedBatch.Cmd.SendEmail.Send(String body, String path, LogManagerBase _Log) in c:\code\Common.FileCollector\Development\Monitoring\LastExecutedBatch.Cmd\SendEmail.cs:regel 38 
 
How can i fix this error ? so the email will be send ??
anyone got any idea ?? thnx  
 
 

Answers (1)