我正试图找出为什么FileSystemWatcher在本地运行的原因,但是当我将其部署到Azure Webapp时却无法运行。
我已验证文件是在webapp文件夹中创建的,但创建文件时看守者似乎未触发。 Azure日志或应用程序日志中没有相关的错误/警告消息。我创建了一个示例应用进行调查,在那里我看到了相同的问题。
public sealed class TestController : Controller
{
private static readonly ILog _log = LogManager.GetLogger(typeof(TestController));
private static readonly string _folder = ConfigurationManager.AppSettings["BasePath"];
private static FileSystemWatcher _watcher;
[HttpGet, Route("start-monitoring")]
public ActionResult StartMonitoring()
{
if (_watcher != null)
return new HttpStatusCodeResult(HttpStatusCode.OK);
_watcher = new FileSystemWatcher(_folder, "*.*") { EnableRaisingEvents = true };
_watcher.Created += (sender, args) =>
_log.Debug($"YAY!!! Watcher triggered, new file found `{args.FullPath}`");
_log.Debug($"Watcher started for folder `{_folder}`");
return new HttpStatusCodeResult(HttpStatusCode.Created);
}
[HttpGet, Route("create-file/{filename}")]
public async Task<ActionResult> CreateFileAsync(string filename)
{
string fullFilename = $"{_folder}\\{filename}.txt";
using (var writer = new StreamWriter(fullFilename))
{
await writer.WriteAsync("Some content").ConfigureAwait(false);
}
_log.Debug($"Created file `{fullFilename}`");
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
}
使用上面的代码,当我转到http://localhost:23481/start-monitoring
时,这将创建监视FileSystemWatcher
文件夹的C:\test
。
然后,当我转到http://localhost:23481/create-file/blahblah
时,将在文件夹blahblah.txt
中创建一个名为C:\test
的文件。日志文件显示正在创建的文件,以及FSW触发器,表明已注意到有新文件出现。
2019-08-15T10:37:54.705Z DEBUG TestProject.TestController
Watcher started for folder `C:\test`
2019-08-15T10:38:02.399Z DEBUG TestProject.TestController
Created file `C:\test\blahblah.txt`
2019-08-15T10:38:02.400Z DEBUG TestProject.TestController
YAY!!! Watcher triggered, new file found `C:\test\blahblah.txt`
2019-08-15T10:38:06.137Z DEBUG TestProject.TestController
Created file `C:\test\another.txt`
2019-08-15T10:38:06.141Z DEBUG TestProject.TestController
YAY!!! Watcher triggered, new file found `C:\test\another.txt`
我将相同的代码部署到Azure,唯一的区别是配置中的基本路径。日志显示FileSystemWatcher
已正确创建,以监视Azure webapp文件系统上的D:\home\site\wwwroot\test
文件夹。
然后,当我转到https://xxx.azurewebsites.net/create-file/blahblah
时,将在文件夹blahblah.txt
中创建一个名为D:\home\site\wwwroot\test
的文件。日志文件显示正在创建的文件,但不显示在创建时触发的FSW:
2019-08-15T10:43:01.681Z DEBUG TestProject.TestController
Watcher started for folder `D:\home\site\wwwroot\test`
2019-08-15T10:43:27.010Z DEBUG TestProject.TestController
Created file `D:\home\site\wwwroot\test\blahblah.txt`
2019-08-15T10:43:33.834Z DEBUG TestProject.TestController
Created file `D:\home\site\wwwroot\test\another.txt`
我也尝试过手动在文件夹中创建文件,但这仍然不会触发!!!日志条目。 FileSystemWatcher不会在Azure中触发是一个已知问题,还是我错过了某些事情?
我已将处理程序分配给FileSystemWatcher支持的所有事件,即已更改/已创建/已删除/已处置/错误/重命名,并且在Azure中对我唯一触发的事件是Changed
。我想我必须做某种黑客来监视Changed事件,我可能可以假设文件是否存在是“ Created”事件,否则是“ Deleted”事件。
答案 0 :(得分:0)
我更改了代码以侦听FileSystemWatcher
支持的所有事件。我发现在Azure上触发的唯一事件是Changed
事件。没有其他事件被调用。我找不到关于此的任何文档,所以可能是错误,也可能是设计的,谁知道。
我现在使用的修复程序在下面,基本上是在Changed事件上,我检查文件是否存在,如果确实存在,我认为这是“文件创建事件”,否则是“文件删除事件”。
[HttpGet, Route("start-monitoring")]
public ActionResult StartMonitoring()
{
if (_watcher != null)
return new HttpStatusCodeResult(HttpStatusCode.OK);
_watcher = new FileSystemWatcher(_folder, "*.*") { EnableRaisingEvents = true };
//none of these events are triggered in Azure
_watcher.Created += (sender, args) =>
_log.Debug($"Watcher triggered `Created`, `{args.FullPath}`");
_watcher.Deleted += (sender, args) =>
_log.Debug($"Watcher triggered `Deleted`, `{args.FullPath}`");
_watcher.Renamed += (sender, args) =>
_log.Debug($"Watcher triggered `Renamed`, `{args.OldFullPath}` to `{args.FullPath}`");
_watcher.Error += (sender, args) =>
_log.Debug($"Watcher triggered `Error`, `{args.GetException().ToString()}`");
_watcher.Disposed += (sender, args) =>
_log.Debug($"Watcher triggered `Disposed`");
//Changed is the only event that gets triggered in Azure
_watcher.Changed += (sender, args) =>
{
if (System.IO.File.Exists(args.FullPath))
_log.Debug($"Watcher triggered `Changed (created)`, `{args.FullPath}`");
else
_log.Debug($"Watcher triggered `Changed (deleted)`, `{args.FullPath}`");
};
_log.Debug($"Watcher started for folder `{_folder}`");
return new HttpStatusCodeResult(HttpStatusCode.Created);
}
这现在对我来说似乎很好:
2019-08-16T08:54:37.419Z DEBUG TestProject.TestController
Watcher started for folder `C:\test`
2019-08-16T08:55:16.625Z DEBUG TestProject.TestController
Created file `C:\test\moo.txt`
2019-08-16T08:55:16.634Z DEBUG TestProject.TestController
Watcher triggered `Changed (created)`, `C:\test\moo.txt`
2019-08-16T08:55:50.096Z DEBUG TestProject.TestController
Created file `C:\test\poop.txt`
2019-08-16T08:55:50.101Z DEBUG TestProject.TestController
Watcher triggered `Changed (created)`, `C:\test\poop.txt`